[英]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.