Python (PyMySQL) SELECT query returning boolean, not desired value

ε祈祈猫儿з 提交于 2019-12-11 08:20:07

问题


I am using PyMySQL with Python to access my database. (MySQLdb is not yet available for newer releases of Python.)

This is my query:

cur = db.cursor()
cur.execute("SELECT ingredientID FROM Ingredients WHERE ingredientName = %s", "onions")

However, instead of returning the ingredientID, a boolean is returned stating the recipe was found. I have worked with MySQL(i) in PHP and have not had this issue occur in the past.


回答1:


You need to somehow fetch the result of the query:

cur = db.cursor()
cur.execute("SELECT ingredientID FROM Ingredients WHERE ingredientName = %s", "onions")
print(cur.fetchone()) # Fetch one row

or

print(cur.fetchall()) # Fetch all rows


来源:https://stackoverflow.com/questions/22767603/python-pymysql-select-query-returning-boolean-not-desired-value

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