1064,“您的SQL语法有错误;…”Python MySQL

2024-03-29 08:20:27 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我从上周五开始就一直在研究这个问题,无法避免这个错误:

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数据库中获取数据,它使用简单的SalesForce在SalesForce中创建一个case,然后查询它正确创建的case,但是我需要它将这个case编号写回我为票证号创建的列中的数据库中。

当前代码:

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替换“order_id=87”并让数据保持在那里(“UPDATE rma_order SET rma_num=%s WHERE order_id=87”%(data))那么它工作正常,并以正确的格式将案例号写入数据库,只要我用%s将“id”作为因子添加,它就会给我错误。我也试过使用%d,结果相同。

任何帮助都将不胜感激。


Tags: theinidfordata错误ordererror
1条回答
网友
1楼 · 发布于 2024-03-29 08:20:27

data值是一个列表,您正在尝试将其格式化为查询。并且,不要使用字符串格式将变量插入查询中-而是使用适当的查询参数化:

cursor.execute("""
    UPDATE 
        tplinkus_rma.rma_order 
    SET 
        rma_num=%s 
    WHERE 
       order_id=%s""", (data[0], Id))

请注意查询参数是如何放置在元组中并作为单独的参数传递的。

相关问题 更多 >