如何将python flask中的变量传递给mysqldb? [英] How to pass variables in python flask to mysqldb?

查看:58
本文介绍了如何将python flask中的变量传递给mysqldb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.py文件中的代码:

Code in .py file:

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"

session["username"] keep a user cookie. The user I already logged in names "admin"

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

But there might be something wrong with the MySQL command inside is_voted

错误:

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

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

But I got the correct return value while using

SELECT TUTOR_VOTED FROM USERS WHERE USERNAME='admin'

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

Is there anything wrong with my input format inside is_voted?

推荐答案

您的组合输出字符串"SELECT TUTOR_VOTED FROM USERS WHERE USERNAME =" + str(session ["username"])未命中几个单引号''.您可以将其更改为:

Your output string of the combination "SELECT TUTOR_VOTED FROM USERS WHERE USERNAME="+str(session["username"]) misses couple of single quote ''. You can change it to:

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

这篇关于如何将python flask中的变量传递给mysqldb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆