使用where var like语句导入sqlite3

时间:2016-11-23 19:42:16

标签: python sql sqlite

我有这段代码:

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的陈述给我带来了麻烦。

我尝试了许多组合,例如"+Title+"{} .format(Title),添加或删除" %...%",但每次收到错误:sqlite3.ProgrammingError: Incorrect number of bindings suppliedsqlite3.OperationalError: near ",": syntax error

1 个答案:

答案 0 :(得分:1)

您必须将%添加到参数:

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