python的SQL语法错误

时间:2017-07-30 14:38:18

标签: python mysql

我有一个我无法解决的问题 我想更新MysQl表中的数据,但我收到此错误:

cursor.execute("UPDATE employees SET PhPath='~/Desktop/test/server/dataSet/%s' WHERE id=%s; ",(generate_names(UserID,1),UserID))
File "/home/chiheb/.virtualenvs/cv/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/home/chiheb/.virtualenvs/cv/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
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 'User.2.1.jpg'' WHERE id=2' at line 1")

这是我的代码的一部分:

                data = recv_msg(conn)
                data = json.loads(data)
                UserName = input("enter user's name: ")
                UserLastName = input("enter user's last name: ")
                UserPost = input("enter user's post: ")
                cursor.execute("INSERT INTO employees VALUES (NULL, %s, %s, %s, %s, NULL);",(UserName, UserLastName, UserPost, data['RasID']))
                db.commit()
                cursor.execute("SELECT LAST_INSERT_ID(); ")
                UserIDL = cursor.fetchone()
                UserID = UserIDL[0]
                JL= data['Jliste']
                for i in range(0,10) :
                    cel = json.loads(JL[i])
                    file_name = generate_names(UserID,i+1)
                    img = base64.b64decode(cel['img'])
                    with open(file_name,'wb') as _file:
                        _file.write(img)
                    print "image {} Received ".format(i+1)
                cursor.execute("UPDATE employees SET PhPath='~/Desktop/test/server/dataSet/%s' WHERE id=%s; ",(generate_names(UserID,1),UserID))
                response = "images Received "
                conn.send(response)
                db.commit()

1 个答案:

答案 0 :(得分:1)

问题是你不能用参数进行部分替换。在代码中生成路径,仅使用“%s”(不带引号)作为值。