python sqlite3 query with AND

mickNeill

I am trying to build a simple Address book GUI that has a wx.listbox, that holds all the names in the book, first and last. Once clicked, it will return the information attached to the name from a database file. Right now I have it working by just the last name, I am trying to match first and last names. I am not, really, familiar with the SQLite 3 commands and syntax.

The function is below, this works fine now, but I want to change the query to something like:

select * from AddressBook where Last like names[0] and First like names[1]

Any help would be great!

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

The query from your comment should work.

Here is a small working example:

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())

It prints:

[('John', 'Smith')]

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ