2014-03-31 126 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