简体   繁体   中英

How to query from sqlite3 database if content in the entry match small requirement

I want to querying data from sqlite3 db from python and populate it in a Listbox but if am doing the search i have to provide the full name like Harvard University before the records can be inserted into the Listbox .

I want to query record like Harvard University by providing only Harvard then it will output all records with Havard content in it for me because if i don't provide full name it will not populate the listbox with any record.

Your suggestions are welcome to achieve this.

import tkinter as tk
import sqlite3

conn = sqlite3.connect("STATS.db")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS institution(id INTEGER PRIMARY KEY, 
name TEXT)")
conn.commit()
conn.close()


def query_record():
    data1 = e1_search.get()

    conn = sqlite3.connect("STATS.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM institution WHERE name=?", (data1,))
    row = cur.fetchall()

    for n in row:
        List.insert(tk.END, n)
        print(n)
    conn.close()


root = tk.Tk()
root.geometry("300x300")

List = tk.Listbox(root, width=100)
List.pack()

e1_search = tk.StringVar()
e1 = tk.Entry(root, textvariable=e1_search)
e1.pack()

b = tk.Button(text="Search", command=query_record)
b.pack(side=tk.BOTTOM)

root.mainloop()

I think you may have to modify the query. Since "SELECT * FROM institution WHERE name=?", (data1,) will exactly match the input string you can try with,

"SELECT * FROM institution WHERE name like ?", (data1+'%',) .

Hope this works!

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