2014-03-31 87 views
1

我使用PyMySQL和Python来访问我的数据库。 (MySQLdb的尚不可用于Python的新版本。)Python(PyMySQL)SELECT查询返回布尔值,不是所需的值

这是我的查询:

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

然而,而不是返回ingredientID,返回一个布尔值,说明配方被发现。我曾经在PHP中使用MySQL(i),并且过去没有发生过这个问题。

+0

如果“布尔”你的意思是“字节的文字”,那么这可能会帮助您:HTTP:// stackoverflow.com/questions/27855037/pymysql-connection-select-query-and-fetchall-return-tuple-that-has-byte-literals/27873654#27873654 –

回答

3

你需要以某种方式获取查询结果:

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

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

Thx!这工作。我唯一的问题是如何从创建的元组中获取值? – gotguts

+0

[这个答案](http://stackoverflow.com/questions/4940670/pymysql-fetchall-results-as-dictionary)解释如何去做 – vaultah

+0

再次感谢,就在你回应之前,我意识到我可以使用fetchone ()本身。 – gotguts