我是python的新手,我正在努力使用mysql命令和参数。具体地说,我发现我必须使用'%‘来分隔命令和参数:
该连接仅作为备注包含,仅供参考。
@app.route("/test")
def test():
# db = pymysql.connect(
# host="localhost",
# user="root",
# password="---------",
# database="golf",
# charset="utf8mb4",
# cursorclass=pymysql.cursors.DictCursor,
# )
cur = db.cursor()
cur.execute("USE golf")
mySQLQuery = "SELECT * FROM users WHERE email='%s'"
email = "test@test.com"
cur.execute(mySQLQuery % (email,))
return render_template("index.html")我发现的示例似乎使用',‘而不是'%',如果我没有弄错的话,可能会推荐使用',’。然而,当我将'%‘替换为',’时,我收到一个错误:
pymysql.err.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 'test@test.com''' at line 1")我所做的唯一更改是在cur.execute命令中:
cur.execute(mySQLQuery, (email,))我认识到我的理解可能是错误的,'%‘是可以接受的。
任何指导或建议都将不胜感激。
发布于 2019-12-03 23:51:06
尝试从“...电子邮件=‘%s’”中删除
只发送电子邮件= %s并使用cur.execute(mySQLQuery,( email,))
希望这能有所帮助
在这里您可以找到一个很好的示例https://stackoverflow.com/a/37094912/12474033
https://stackoverflow.com/questions/59160663
复制相似问题