python sqlite3查询与

米克·尼尔

我正在尝试构建一个简单的地址簿GUI,该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
Gosha F

您评论中的查询应该起作用。

这是一个小的工作示例:

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')]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章