我想插入一个字符串
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')
最好使用查询参数来避免引用和其他问题:
cur.execute("INSERT INTO inv values (%s, %s)", (str(row[1]), str(row[2])))
如果您的模型稍后更改,也可以为列命名,这也是最佳做法:
"INSERT INTO inv (col1, col2) VALUES (%s, %s)"