Using MySQL keywords in a query?

I am trying to make a MySQL query with keywords in it. Is there any way to encapsulate these keywords so that I can use them anyway?

Here's the query that won't execute:

INSERT INTO PendingPasswordRecovery (key, userID, TimeModified, TimeCreated, TimeDeleted, IsDeleted) VALUES ('test123', 1, '2011-07-26 16:26:56', '2011-07-26 16:26:56', '1000-01-01 00:00:00', false)

As you have probably guessed already, "key" is the keyword that I am using in the query, and it is unfortunately also the name of the column I want to use.

Upvotes: 2

Views: 2797

Answers (4)

ruddy
ruddy

Reputation: 145

I don't think there is a way to do so. Try key but the better way is always to rename your field

Upvotes: -1

Lukasz Wozniak
Lukasz Wozniak

Reputation: 91

use ` to escape column names:

INSERT INTO PendingPasswordRecovery (`key`, `userID`, `TimeModified`, `TimeCreated`, `TimeDeleted`, `IsDeleted`) VALUES ('test123', 1, '2011-07-26 16:26:56', '2011-07-26 16:26:56', '1000-01-01 00:00:00', false)

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270607

Just enclose them in backquotes

INSERT INTO PendingPasswordRecovery (`key`, userID, TimeModified, TimeCreated, TimeDeleted, IsDeleted) VALUES ('test123', 1, '2011-07-26 16:26:56', '2011-07-26 16:26:56', '1000-01-01 00:00:00', false)
                                   ^^^^^^^^^

Here's the list of MySQL reserved keywords.

Upvotes: 2

Brendan Bullen
Brendan Bullen

Reputation: 11798

Use backticks:

INSERT INTO PendingPasswordRecovery (`key`, `userID`, `TimeModified`, `TimeCreated`, `TimeDeleted`, `IsDeleted`) VALUES ('test123', 1, '2011-07-26 16:26:56', '2011-07-26 16:26:56', '1000-01-01 00:00:00', false)

I have added backticks to all column names but you could just do it for the keyword. The backticks are called "identifier quote characters". For more information, see: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html and http://dev.mysql.com/doc/refman/5.1/en/identifiers.html

Upvotes: 4

Related Questions