How to pass variables in python flask to mysqldb?

风流意气都作罢 提交于 2021-01-29 05:21:44

问题


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"] keep a user cookie. The user I already logged in names "admin"

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

Error:

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 there anything wrong with my input format inside is_voted?


回答1:


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"]))


来源:https://stackoverflow.com/questions/55980081/how-to-pass-variables-in-python-flask-to-mysqldb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!