gpt4 book ai didi

带有AND的python sqlite3查询

转载 作者:太空宇宙 更新时间:2023-11-04 05:10:09 26 4
gpt4 key购买 nike

我正在尝试构建一个简单的地址簿 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')]

关于带有AND的python sqlite3查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43208807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com