user6364361
user6364361

Reputation:

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?

Upvotes: 0

Views: 139

Answers (3)

Norbert
Norbert

Reputation: 6084

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`

Upvotes: 4

Bernd Buffen
Bernd Buffen

Reputation: 15057

If you use reserved Keywords as Column or Table Names you must quote them with backticks like this:

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

Upvotes: 3

Joe Stefanelli
Joe Stefanelli

Reputation: 135858

int is a keyword, so you must escape it with backticks.

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

Upvotes: 3

Related Questions