[英]Make Advanced Searches in SQLite Data Base
我得到了一個帶有人名的地址簿。 我使用以下代碼在名稱列中執行搜索:
register = []
text = "{0}%" .format(self.lineEdit_6.text())
c.execute("select id, name from contacts where nome like '{0}'" .format(text))
for row in c:
register.append(row)
self.listWidget_4.addItem(str(row[1]))
self.listWidget_6.addItem(str(row[0]))
if register == []:
self.listWidget_4.addItem("No result")
名稱列包含名稱和姓氏(例如John Smith),當我不記得全名而只記得起始字母時,以上代碼可以正常工作。 但我希望結果顯示所有匹配項,而不僅僅是名稱的開頭。 因此,如果我得到3個名字(Simonne Welsh,Adam Loyd,John Smith)和一個字母S,則必須給出名字Simone Welsh和John Smith的結果。 我怎么做?
做這個:
text = "%{0}%" .format(self.lineEdit_6.text())
c.execute("select id, name from contacts where nome like '{0}'" .format(text))
(在匹配字符串之前和之后都包含通配符)。
另外,請考慮使用( 注意:此語法已更正 ):
c.execute("select id, name from contacts where nome like ?", [text])
要么
c.execute("select id, name from contacts where nome like ?", (text, ))
這將:
使您的代碼免受SQL注入攻擊。
正確處理text
包含特殊字符的情況,例如單引號(例如O'Reilly
名稱)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.