Python pymySQL字符串引号

问题描述 投票:1回答:1

我想插入一个字符串

insert_cmd = "INSERT INTO inv values (\""+str(row[1])+"\",\""+str(row[2])+"\")
(cur.execute(insert_cmd))

错误信息:

pymysql.err.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 \'google search appliance"","active","D\' at line 1')
python mysql python-3.x pymysql
1个回答
3
投票

最好使用查询参数来避免引用和其他问题:

cur.execute("INSERT INTO inv values (%s, %s)", (str(row[1]), str(row[2])))

如果您的模型稍后更改,也可以为列命名,这也是最佳做法:

"INSERT INTO inv (col1, col2) VALUES (%s, %s)"
© www.soinside.com 2019 - 2024. All rights reserved.