MySQL Syntax Error “right syntax to use near 'desc” [duplicate]

余生长醉 提交于 2019-11-27 09:05:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!