简体   繁体   中英

SQLite3 (python) query to search multiple columns in one SELECT statement

I'm trying to create an address book and I'd need to check if a given input can be found in all of three columns of the table i created:

conn = sqlite3.connect("Address_Book.db")
c = conn.cursor()

c.execute("""CREATE TABLE addresses (
        name text,
        number text,
        email text)
    """)

    conn.commit()

c.execute("SELECT rowid, * FROM addresses WHERE name LIKE (?)", ('%' + self + '%',))          
res = c.fetchall()

In the above code I'm only searching the 'name' column. I tried writing 'name, number, email' but maybe I don't know the correct syntax and couldn't find it online. I found a way around the problem but it's not that elegant, so I hope someone can help, thank you in advance.

A simple workaround for your SQL query would be using the OR operator. As an example, please have a look at the following query

SELECT * from addresses where name LIKE '%A%'
OR number LIKE '%A%'
OR email LIKE '%A%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM