Devon Quick
Devon Quick

Reputation: 348

C# MySQL Syntax error in SQL Statement

I have this simple method that is supposed to insert a row into a DB. It is throwing an exception.

private void AddToLiveQueue(int user_id, bool release = false)
    {
        string sql = "INSERT INTO live_support_queues (user_id, release, created_at, updated_at)";
        sql += " VALUES(?user_id, ?release, ?created_at, ?created_at)";

        MySqlConnection conn = new MySqlConnection(connString);
        MySqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = sql;

        cmd.Parameters.AddWithValue("?user_id", user_id);
        cmd.Parameters.AddWithValue("?release", release);
        cmd.Parameters.AddWithValue("?created_at", DateTime.UtcNow);


        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch (Exception ex)
        {
            SendEmail email = new SendEmail();
            email.Send(ex.Message + "      " + ex.ToString());
        }
    }

I am getting this error:

"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 'release, created_at, updated_at) VALUES(70, 0, '2017-09-22 23:00:16.686741', '20' at line 1"

Here is the SQL columns

Any help is greatly appreciated. Thanks!

Upvotes: 0

Views: 137

Answers (1)

Uueerdo
Uueerdo

Reputation: 15941

release is a reserved word, and needs escaped with ` symbols if used as an identifier.

https://dev.mysql.com/doc/refman/5.7/en/keywords.html

Upvotes: 3

Related Questions