0

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.

1
  • There's a facility for storing queries in the database: Stored procedures. Storing SQL in a database is rarely a good idea. Commented Oct 28, 2013 at 16:43

2 Answers 2

5

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)
Sign up to request clarification or add additional context in comments.

3 Comments

While I guessed immediately that that was the problem, I had no recollection until I read the error message that SQL was a reserved word. Why is it reserved?
I don't know. Maybe it's used in triggers or stored functions?
Thanks, I noticed it was a reserved word right after posting this, but I didn't know that I could still use it with the backticks. Thanks for that.
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.