0

I have a table named 'accounts' with a column called 'Rank' in my MySQL database. 'Rank' is a Length 2 INT type column.

My code for running my MySQL command is:

Command = CreateQuery();
Command.CommandText = "INSERT INTO accounts ([Rank]) VALUES (0)";
Command.ExecuteNonQuery();

However, when I attempt to run it I get this error:

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 'Rank) VALUES (0)' 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 (***) in (***)Database.cs:line 113

Can anyone point out what is wrong with my syntax?

Jaskier
  • 1,075
  • 1
  • 10
  • 33
vxern
  • 3
  • 3
  • Look here for reserved words in table column names, Rank is a reserved word: https://stackoverflow.com/questions/2889871/how-do-i-escape-reserved-words-used-as-column-names-mysql-create-table – Brad Mar 04 '19 at 20:26
  • @Brad are you sure? In anycase the OP has used square brackets around it. The strange thing however is the error message. There are no square brackets there. – Steve Mar 04 '19 at 20:30
  • I am not certain, been a while for MySQL, tried to give something to look into based off general SQL knowledge and googling reserved words in MySQL. Usually you can use the brackets, and the syntax looks fine, but it being a keyword MySQL may treat it differently and the link may take care of it – Brad Mar 04 '19 at 20:32
  • 2
    Doesn't MySQL use backquotes instead of brackets for escaping reserved words? – Dour High Arch Mar 04 '19 at 20:33
  • 1
    @DourHighArch I tried backquotes and sure enough, they worked! Thanks! – vxern Mar 04 '19 at 20:40

0 Answers0