我正在尝试构建一个简单的地址簿 GUI,它有一个 wx.listbox
,它包含地址簿中的所有姓名,包括名字和姓氏。单击后,它将返回数据库文件中附加到名称的信息。现在我只用姓氏来工作,我正在尝试匹配名字和姓氏。我真的不熟悉 SQLite 3 命令和语法。
函数如下,现在可以正常工作,但我想将查询更改为:
select * from AddressBook where Last like names[0] and First like names[1]
任何帮助都会很棒!
def onListBox(self, event):
name = event.GetEventObject().GetStringSelection()
names = name.split(',')###names[0]=Last name, names[1] = first name
cursor = db.cursor()
cursor.execute("select * from AddressBook where Last like ?",('%'+names[0]+'%',) )
result = cursor.fetchall()
return result
您评论中的查询应该有效。
这是一个小的工作示例:
import sqlite3
conn = sqlite3.connect("test.sql")
cursor = conn.cursor()
cursor.execute("create table address_book (first_name text, last_name text)")
names = [["John", "Smith"], ["Jane", "Smith"]]
for first_name, last_name in names:
cursor.execute("insert into address_book (first_name, last_name) values (?, ?)", (first_name, last_name))
cursor.execute("select * from address_book where first_name like ? and last_name like ?", ("%" + names[0][0] + "%", "%" + names[0][1] + "%"))
print(cursor.fetchall())
它打印:
[('John', 'Smith')]
我是一名优秀的程序员,十分优秀!