madman2890
madman2890

Reputation: 1992

MySQL sql as column

I've got the following table

db.queries
| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name      | varchar(256)     | NO   |     |         |                |
| sql       | text             | NO   |     | NULL    |                |
| frequency | int(10) unsigned | NO   |     | 86400   |                |

I am inserting values which are as labeled, a name and a sql query like so using the MySQLdb python tool.

cur.execute("insert into db.queries (name, sql, frequency) values ('{0}', '{1}', {2})".format('some query', 'Select a query', 86400)

I've used MySQL and MySQLdb many times before and never have had these issues.

The error I'm constantly getting is as follows:

_mysql_exceptions.ProgrammingError: (1064, "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 'sql, frequency) values ('MY NAME', 'Select count(*) as' at line 1")

I appreciate any help.

Upvotes: 0

Views: 72

Answers (2)

elixenide
elixenide

Reputation: 44851

sql is a reserved word in MySQL. Put it in backticks, like so:

cur.execute("insert into db.queries (name, `sql`, frequency) values ('{0}', '{1}', {2})".format('some query', 'Select a query', 86400)

Upvotes: 1

Barmar
Barmar

Reputation: 782775

sql is a reserved word in MySQL, you need to put it in backticks:

cur.execute("insert into db.queries (name, `sql`, frequency) values ('{0}', '{1}', {2})".format('some query', 'Select a query', 86400)

Upvotes: 5

Related Questions