Fayzan Naufal
Fayzan Naufal

Reputation: 17

Tkinter entry output

I am creating a Data finder using a db file and i use tkinter entry. however in the console it returns the output [] and the code is this

import sqlite3
import tkinter

db = sqlite3.connect ('covidjakartadb.db')

window = tkinter.Tk()
window.geometry("500x300")

label = tkinter.Label(window, text="Please enter a area")
label.pack()

entry = tkinter.Entry(window)
entry.pack()

select_all = "SELECT * FROM locations WHERE '%{0}%'".format(entry)

cursor = sqlite3.Cursor(db)

cursor.execute(select_all)

def Search_Completed():
    label = tkinter.Label(window,  text="aaaaa")

Button = tkinter.Button(window, text="Search data", command=Search_Completed)
Button.pack()



positive = cursor.fetchall()

print (positive)

window.mainloop()

This project is due today so a answer today would be greatful

Upvotes: 0

Views: 189

Answers (1)

Tls Chris
Tls Chris

Reputation: 3824

The code to select from the database needs to be in the search_completed function. As written it runs before the GUI is even open.

The search should be using the contents of the Entry, entry.get() not entry.

import sqlite3
import tkinter

db = sqlite3.connect ('covidjakartadb.db')

window = tkinter.Tk()
window.geometry("500x300")

label = tkinter.Label(window, text="Please enter a area")
label.pack()

entry = tkinter.Entry(window)
entry.pack()

def Search_Completed():
    # select_all = "SELECT * FROM locations WHERE '%{0}%'".format(entry)
    # select_all = "SELECT * FROM locations WHERE '%{0}%'".format( entry.get() )
    # This will work
    select_all = "SELECT * FROM locations WHERE City LIKE '%{0}%'".format( entry.get() )        
    # Or this.
    # select_all = "SELECT * FROM locations WHERE City == '{0}'".format( entry.get() )
    cursor = sqlite3.Cursor(db)
    cursor.execute(select_all)
    positive = cursor.fetchall()
    print (positive)
    label = tkinter.Label(window,  text="aaaaa")

Button = tkinter.Button(window, text="Search data", command=Search_Completed)
Button.pack()
window.mainloop()

I'm unable to test the code as I don't have the database so this may have a few errors in it.

Upvotes: 1

Related Questions