gotguts gotguts - 1 month ago 9
Python Question

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

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.

Answer

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