我正在尝试搜索像变量这样的数据,但我想查找像%?%这样的数据,但是这当然不起作用,我可以使用LIKE吗?但我还需要找到所有值,例如D并附带Dog
search=self.text
with sqlite3.connect('Data.db') as db:
user_find = db.cursor()
user_find.execute(
'select * from "Animal Data" where Animal_ID LIKE ? or Animal_Name LIKE ? or Animal_Type LIKE ? or Animal_Gender LIKE ? or Animal_breed LIKE ? or Animal_breed LIKE ? or Animal_breed LIKE? or Animal_colour LIKE ? or Address LIKE ?'
,(search,search,search,search,search,search,search,search,search,))
data = user_find.fetchall()
print(data)
答案 0 :(得分:2)
您应该将要显示在?
语句中的整个表达式绑定到LIKE
占位符。也就是说,如果您要搜索具有字母D
的任何字段,则可以将字符串%D%
绑定到语句。
search = '%' + search + '%'
user_find = db.cursor()
user_find.execute(
'select * from "Animal Data" where Animal_ID LIKE ? or Animal_Name LIKE ? or Animal_Type LIKE ? or Animal_Gender LIKE ? or Animal_breed LIKE ? or Animal_breed LIKE ? or Animal_breed LIKE? or Animal_colour LIKE ? or Address LIKE ?',
(search,search,search,search,search,search,search,search,search,)
)
data = user_find.fetchall()
print(data)