[英]Error inserting value with special character into mysql table using 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")
是什么導致此語法問題?
插入字符串值時使用“單引號”
UPD:
將第二行更改為此
lst2 =['888', '1213726720', '61440', '65535', '\'\\SystemRoot\\System32\\smss.exe\'', '\'wean\'']
因為,這是錯誤的SQL查詢:
INSERT INTO test (Path) VALUES(\SystemRoot\System32\smss.exe)
然后您的代碼會生成這樣的查詢。
您應該引用varchar值:
INSERT INTO test (Path) VALUES('\SystemRoot\System32\smss.exe')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.