Naveh Naveh - 6 months ago 16
SQL Question

Column name int cannot be selected

I've downloaded a source with a lot of code and tables, in one of the tables I got a column named "int" (The source is big and changing the column name isn't the solution for me).

Anyway, I can't print the data on my website because of the column name! Any other column in the table works except the column 'int'. It tells me this error:


You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'int FROM characters WHERE id = '30005'' at line 1]


I tried to select it like this:

"SELECT int FROM characters WHERE id = '" + userId + "'";


And I tried like this:

string selectIntSql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'moopledev' AND TABLE_NAME = 'characters' AND ORDINAL_POSITION = 11";

string selectInt = SqlConnector.printData(selectIntSql);
"SELECT " + selectInt + " FROM characters WHERE id = '" + userId + "'";


Nothing worked.
Any idea what to do?

Answer

SQL (any SQL) contains reserved words, which you better not use as a column name. INT is a very obvious one.

The way around this is:

  • Use a better column name
  • or use backticks around your column

Example for the backticks:

 `int`