遇到了使用動態列表將值插入到mysql表中的問題。我一直有一個問題,指向一個表而不是一個值的引號。使用python將特殊字符的值插入到mysql表中時出錯
下面是代碼:
lst = ['Pid', 'Base', 'Size', 'LoadCount', 'Path','Casename']
lst2 =['888', '1213726720', '61440', '65535', '\\SystemRoot\\System32\\smss.exe', 'wean']
table_name ="test"
insert_intotable = "INSERT INTO " + table_name + "(" + ",".join(lst) + ") VALUES (" + ",".join(lst2) + ")"
print insert_intotable
c.execute(insert_intotable)
conn.commit()
c.close()
conn.close()
這會導致以下錯誤:
> Traceback (most recent call last): File "pp.py", line 53, in
> <module>
> c.execute(insert_intotable) 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
> '\\SystemRoot\\System32\\smss.exe,test)' at line 1")
是什麼原因造成的語法問題?
你必須使用由MySQL適配器提供的字符串格式化的記載:https://dev.mysql.com/doc/connector-python/en/connector- python-api-mysqlcursor-execute.html –