【问题标题】:(1064, "You have an error in your SQL syntax; .. for the right syntax to use near '%s, %s, %s, %s, %s)' at line 1") [duplicate](1064,“您的 SQL 语法有错误;.. 在第 1 行的 '%s, %s, %s, %s, %s)' 附近使用正确的语法”)[重复]
【发布时间】:2019-01-28 19:07:12
【问题描述】:

我正在尝试将数据插入到我的 mysql 数据库中,但我总是收到参数有问题的错误,我想将其添加到我的插入语句中,我尝试了各种解决方案和一个'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")

【问题讨论】:

  • 您不应该使用format 将参数插入到您的查询中。试试cursor.execute(insert_cmd, tpa)

标签: python mysql flask


【解决方案1】:

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

cursor.execute(insert_cmd, tpa)

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

【讨论】:

  • 非常感谢,现在当我渲染页面时它可以工作,但是当我按下页面上的提交按钮(它在表单中)并单击时,字符串元组应该被插入到数据库,我得到内部服务器错误。你知道为什么吗?谢谢
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 2014-11-02
  • 2021-01-07
相关资源
最近更新 更多