问题
I'm working in Python and using the MySQLdb module. I have a working connection (I can run other queries successfully)
c.execute("ALTER TABLE results ADD COLUMN desc TEXT")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 35,
in defaulterrorhandler
raise errorclass, errorvalue
I'm getting the following error:
_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 'desc TEXT' at line 1")
I've had similar trouble before; MySQLdb's syntax error messages are terribly non-descriptive.
How can this be fixed?
回答1:
I believe desc
is reserved. It is used in an ORDER BY
clause
You may be able to get away with using it if you put back-ticks around it, but I think you would be better off changing the name to a non-reserved word.
回答2:
desc
is a reserved keyword in MySQL - documented here: http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
If you insist on using desc
you should escape it using the back tick character (`) - as described here: How do I escape reserved words used as column names? MySQL/Create Table
I'll recommend you to change the desc
column name to e.g. description
, as I believe it is best practice never to include reserved keywords in the db schema.
来源:https://stackoverflow.com/questions/8303827/mysql-syntax-error-right-syntax-to-use-near-desc