2017-04-04 69 views
2
查詢

我試圖建立一個簡單的地址簿GUI,有一個wx.listbox,它包含本書中所有的名字,第一個和最後一個。點擊後,它會從數據庫文件中返回附加到名稱的信息。現在我的工作只是姓氏,我想匹配名字和姓氏。我並不真的熟悉SQLite 3的命令和語法。python sqlite3與AND

功能如下,現在這個工作得很好,但我想查詢更改爲類似:

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 
+0

你嘗試查詢? – glibdud

+0

您是否嘗試將第二個參數添加到查詢中? –

+0

是的,它回來了一個語法錯誤 – mickNeill

回答

1

您的評論的查詢應該工作。

這裏是一個小工作示例:

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