C# Mysql query always giving syntax error when using 'addwithvalue' with an int

-1

SOLUTION Okey so the solution was that the name "interval" seems to be used for something else in MySQL, changing the column name solved it.

EDIT The error I receive is:

Exception thrown: 'MySql.Data.MySqlClient.MySqlException' in MySql.Data.dll
MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'interval) VALUES('20180307', '12:36:29.0', '01:03:51.9', '5')' at line 1
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
   at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
   at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at HeartRateApp.HrmReader.UploadToDatabase(HRM hrm) in C:\Users\Emil Vidmark\source\repos\HeartRateApp\HeartRateApp\HrmReader.cs:line 77

I want to extract data from a file and insert the data into a database. It is working fine as long as I have strings that I add into the query but when I want to add int:s it gives me syntax error.

When I'm trying to use the function 'addwithvalue' for the '@interval' in my syntax it says that I have error in the syntax. And it is only when I'm tryng to add the 'interval' to the query it happens.

Is there anyone out there who has a solution to the problem?

        private void UploadToDatabase(HRM hrm)
    {
        //The string with the info to the db.
        string connectionString = "Server=127.0.0.1;Port=3306;Database=mydb;Uid=root;password=root";
        MySqlConnection connection = new MySqlConnection(connectionString);
        MySqlCommand command = connection.CreateCommand();

        command.CommandText = "INSERT INTO training_sessions(date, start_time, length, interval) VALUES(@date, @start_time, @length, @interval)";
        command.Parameters.AddWithValue("@date", hrm.GetDate());
        command.Parameters.AddWithValue("@start_time", hrm.GetStartTime());
        command.Parameters.AddWithValue("@length", hrm.GetLength());
        command.Parameters.AddWithValue("@interval", hrm.GetInterval());
       // command.Parameters.AddWithValue("@maxhr", hrm.GetMaxHR());
       // command.Parameters.AddWithValue("@resthr", hrm.GetRestHR());
       // command.Parameters.AddWithValue("@vo2max", hrm.GetVO2Max());
       // command.Parameters.AddWithValue("@weight", hrm.GetWeight());
       // command.Parameters.AddWithValue("@runningindex", hrm.GetRunningIndex());

        try
        {
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
        }
    }

The class HRM is looking like this:

    class HRM
{
    private string Date;
    private string StartTime;
    private string Length;
    private int Interval;
    private int MaxHR;
    private int RestHR;
    private int VO2Max;
    private int Weight;
    private int RunningIndex;
    private int[] HRValues;
    public HRM(string date, string startTime, string length, int interval, 
    int maxHR, int restHR, int VO2Max, int weight, int runningIndex, int[] 
    HRValues)
    {
        this.Date = date;
        this.StartTime = startTime;
        this.Length = length;
        this.Interval = interval;
        this.MaxHR = maxHR;
        this.RestHR = restHR;
        this.VO2Max = VO2Max;
        this.Weight = weight;
        this.RunningIndex = runningIndex;
        this.HRValues = HRValues;

    }
    public string GetDate()
    {
        return this.Date;
    }

    public string GetStartTime()
    {
        return this.StartTime;
    }

    public string GetLength()
    {
        return this.Length;
    }

    public int GetInterval()
    {
        return this.Interval;
    }

    public int GetMaxHR()
    {
        return this.MaxHR;
    }

    public int GetRestHR()
    {
        return this.RestHR;
    }

    public int GetVO2Max()
    {
        return this.VO2Max;
    }

    public int GetWeight()
    {
        return this.Weight;
    }

    public int GetRunningIndex()
    {
        return this.RunningIndex;
    }

    public int[] GetHRValues()
    {
        return this.HRValues;
    }

}

}

c#
mysql
asked on Stack Overflow Jan 10, 2019 by E.Vid • edited Jan 10, 2019 by E.Vid

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0