Having an issue creating table using dynamic list. I keep getting error next to Exit as shown:
Traceback (most recent call last): File "pp.py", line 54, in
c.execute(createsqltable) File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in
execute
self.errorhandler(self, exc, value) File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in
defaulterrorhandler
raise errorclass, errorvalue
_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 'Exit VARCHAR(250),caseid
VARCHAR(250))' at line 1")
here is the code:
lst =['Offset', 'Name', 'PID', 'PPID', 'Thds', 'Hnds', 'Sess', 'Wow64', 'Start', 'Exit', 'caseid']
table_name = "test"
createsqltable = """CREATE TABLE IF NOT EXISTS """ + table_name + " (" + " VARCHAR(250),".join(lst) + " VARCHAR(250))"
#print createsqltable
c.execute(createsqltable)
conn.commit()
解决方案
Since Exit is the keyword of sql, you must use `` to escape first.
lst =['Offset', 'Name', 'PID', 'PPID', 'Thds', 'Hnds', 'Sess', 'Wow64', 'Start', '`Exit`', 'caseid']
table_name = "test"
createsqltable = """CREATE TABLE IF NOT EXISTS """ + table_name + " (" + " VARCHAR(250),".join(lst) + " VARCHAR(250))"
#print createsqltable
c.execute(createsqltable)
conn.commit()