2016-11-23 209 views
0

進口的sqlite3我有這樣的代碼:與其中var LIKE語句

Title= "somewords" 
itemID = somenumbers 
import sqlite3 
db = sqlite3.connect('C:\\db.sqlite') 
cursor = db.cursor() 
cursor.execute('SELECT itemID FROM itemAttachments WHERE path LIKE "%?%" ', (Title,)) 
cursor.execute('SELECT key FROM items WHERE itemID =? ', (itemID,)) 

第二條語句與where item=?作品,但第一個與like讓我煩惱。

我已經嘗試了許多combinaisons像"+Title+"{} .format(Title),添加或刪除「%...%"但每次我得到一個錯誤:要麼sqlite3.ProgrammingError: Incorrect number of bindings suppliedsqlite3.OperationalError: near ",": syntax error

回答

1

你必須在%添加到參數:

cursor.execute('SELECT itemID FROM itemAttachments WHERE path LIKE ?', ('%{}%'.format(Title),))