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

2024-10-03 13:22:58 发布

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

我试图将数据插入我的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")

Tags: cmd数据库executeyourreturn错误mysqlconn
1条回答
网友
1楼 · 发布于 2024-10-03 13:22:58

您应该将查询参数作为元组传递给cursor.execute(),而不是插入到查询中,即:

cursor.execute(insert_cmd, tpa)

查询中的%s用作单个参数的占位符。你知道吗

相关问题 更多 >