像变量一样搜索数据,但我想查找像 %?% 这样的数据,但这当然行不通
search for data LIKE a variable but i want to find data like %?% but of course this doesn't work
我正在尝试搜索 LIKE 变量的数据,但我想查找 %?% 之类的数据,但这当然行不通,我可以使用 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)
您应该将要出现在 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)
我正在尝试搜索 LIKE 变量的数据,但我想查找 %?% 之类的数据,但这当然行不通,我可以使用 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)
您应该将要出现在 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)