所以我从上周五开始就一直在研究这个问题,但无法解决这个错误:
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 '[u'161010-035670'] WHERE order_id=87' at line 1" or something along the same lines as this error.
基本上我的 python 将从 MySQL 数据库中获取数据,它使用 Simple-Salesforce 在 SalesForce 中创建一个案例,然后查询它正确创建的案例,但我需要它将该案例编号写回到我专门创建的列中的数据库中为票号。
当前代码:
for rowx in xrange(1, sheet.nrows):
SN = sheet.row_values(rowx, start_colx=3, end_colx=None)[0]
print SN
Id = sheet.row_values(rowx, start_colx=6, end_colx=None)[0]
print Id
d = sf.query("SELECT CaseNumber FROM Case WHERE Serial_Number__c ='%s' AND Status = 'New Portal RMA'" % SN)
data = [e["CaseNumber"] for e in d["records"]]
print (data)
try:
con = MySQLdb.connect(user=ur, passwd=pd, host=ht, port=pt, db=db)
cursor = con.cursor()
cursor.execute("UPDATE rma_order SET rma_num=%s WHERE order_id=%s" % (data, Id))
con.commit()
except Error as error:
print(error)
finally:
cursor.close()
con.close()
主要问题在于这行代码:
cursor.execute("UPDATE rma_order SET rma_num=%s WHERE order_id=%s" % (data, Id))
我试过使用和不使用 '%s' 都没有区别,试过 "...WHERE order_id=%s", (data, Id)) 也有同样的错误。如果我用 cursor.execute("UPDATE rma_order SET rma_num=%s WHERE order_id=87"% (data)) 替换 "order_id=87"并让数据保留在那里,那么它工作正常并以正确的格式将案例编号写入数据库,只要我将“Id”添加为 %s 的一个因素,它就会给我错误。我也尝试过使用 %d 得到相同的结果。
如有任何帮助,我们将不胜感激。
最佳答案
data
值是一个列表,您正在尝试将其格式化为查询。并且,不要使用字符串格式将变量插入查询 - 请改用适当的查询参数化:
cursor.execute("""
UPDATE
tplinkus_rma.rma_order
SET
rma_num=%s
WHERE
order_id=%s""", (data[0], Id))
请注意查询参数是如何放置在元组中并作为单独的参数传递的。
关于python - 1064、 "You have an error in your SQL syntax;..."Python MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39965697/