简体   繁体   English

使用python将带有特殊字符的值插入mysql表时出错

[英]Error inserting value with special character into mysql table using python

Having an issue inserting a value into a mysql table using a dynamic list. 使用动态列表将值插入mysql表时出现问题。 I keep having a problem with the quotes pointing to a table instead of a value. 我一直在用引号指向一个表而不是一个值的问题。

Here is the code: 这是代码:

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()

This causes the following error: 这将导致以下错误:

> 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")

What is causing this syntax issue? 是什么导致此语法问题?

Use 'single quotes' when inserting string values 插入字符串值时使用“单引号”

UPD: UPD:

Change second line to this 将第二行更改为此

 lst2 =['888', '1213726720', '61440', '65535', '\'\\SystemRoot\\System32\\smss.exe\'', '\'wean\'']

Because, it is wrong SQL query: 因为,这是错误的SQL查询:

 INSERT INTO test (Path) VALUES(\SystemRoot\System32\smss.exe)

And your code generates such query. 然后您的代码会生成这样的查询。

You should quote varchar values: 您应该引用varchar值:

 INSERT INTO test (Path) VALUES('\SystemRoot\System32\smss.exe')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM