如何将python flask中的变量传递给mysqldb?

时间:2019-05-04 06:24:14

标签: python mysql flask

.py文件中的代码:

cur = mysql.connection.cursor()
# Check if this user had voted for somebody
is_voted = cur.execute("SELECT TUTOR_VOTED FROM USERS WHERE USERNAME="+str(session["username"]))

session["username"]保留用户cookie。我已经登录的用户名为“ admin”

但是is_voted内部的MySQL命令可能有问题

错误:

MySQLdb._exceptions.OperationalError: (1054, "Unknown column 'admin' in 'where clause'")

但是我在使用时得到了正确的返回值

SELECT TUTOR_VOTED FROM USERS WHERE USERNAME='admin'

enter image description here

is_voted中的输入格式是否有问题?

1 个答案:

答案 0 :(得分:1)

您的组合"SELECT TUTOR_VOTED FROM USERS WHERE USERNAME="+str(session["username"])的输出字符串缺少一对单引号''。您可以将其更改为:

is_voted = cur.execute("SELECT TUTOR_VOTED FROM USERS WHERE USERNAME='%s'" % str(session["username"]))