我想查詢我的sqlite3數據庫並使用列表中的值。這裏是我的代碼:Python SQL Select從列表變量中選擇語句?
for i in range(len(infolist)):
result = cursor.execute('SELECT COUNT(DISTINCT col1)
FROM tablename
WHERE col2 = ?', (infolist[i]))
我收到此錯誤:
ProgrammingError: 'Incorrect number of bindings supplied. The current statement uses 1, and there are 22 supplied.'
,該字符串具有22個字符這就解釋了爲什麼有22個綁定。很明顯,我沒有正確地將字符串傳遞到SQL語句中。
使用'IN'條款,如果陣列可以變成一個逗號分隔的列表,將工作更好地與緩解了循環的需要。 – 2011-05-20 01:54:44