(1064,“您的SQL语法有错误; ..在第1行使用正确的语法以在'%s,%s,%s,%s,%s)'附近使用”)

时间:2018-08-22 13:40:12

标签: python mysql flask

我正在尝试将数据插入到我的mysql数据库中,但是我总是得到以下错误:我想添加到我的insert语句中的参数有问题,我尝试了各种解决方案,在使用“ s”,“ s”的情况下,它已插入到我的数据库中。

有人知道什么是错的吗?

谢谢。

try:
    conn = mysql.connect()
    cursor = conn.cursor()
    insert_cmd = "INSERT INTO TBTAB VALUES(%s, %s, %s, %s, %s)"
    #insert_cmd = "INSERT INTO TBTAB VALUES('s', 's', 's', 's', 's')"
    # insert_cmd = "INSERT INTO TBTAB VALUES(?, ?, ?, ?, ?)"
    tpa= ("szilva", "barack", "meggye", "alma", "korte")
    #cursor.execute("INSERT INTO TBTAB (UserName, Email, TextBoxCont, NodeChosen, CurDate) VALUES ('Caal', 'TomErichsen', 'Svanger', '400esd6', 'Nqdwasorway');")
    cursor.execute(insert_cmd.format(tpa))
    conn.commit()
    return render_template('form.html')
except Exception as e:
    return str(e)

错误:

(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 '%s, %s, %s, %s, %s)' at line 1")

1 个答案:

答案 0 :(得分:4)

You should pass the query parameters as a tuple to cursor.execute() instead of interpolating them into the query, i.e.:

cursor.execute(insert_cmd, tpa)

%s in the query serves as a placeholder for a single parameter.