Searching SQLite database using Python and PySimpleGUI

Posted by


In this tutorial, we will learn how to search an SQLite database using Python with PySimpleGUI. SQLite is a lightweight database that does not require a separate server process to be run. PySimpleGUI is a Python GUI framework that makes it easy to create simple and user-friendly interfaces.

Before we get started, make sure you have Python and PySimpleGUI installed on your computer. You can install PySimpleGUI using pip by running the following command in your terminal:

pip install PySimpleGUI

Next, we need to create an SQLite database and insert some data into it. We will create a simple database that stores information about students, including their name, age, and grade.

import sqlite3

# Create a connection to the database
conn = sqlite3.connect('students.db')

# Create a cursor object
cursor = conn.cursor()

# Create a table to store the student data
cursor.execute('''CREATE TABLE students
                 (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, grade TEXT)''')

# Insert some data into the table
cursor.execute("INSERT INTO students (name, age, grade) VALUES ('Alice', 18, 'A+'),"
                                                             "('Bob', 20, 'B-'),"
                                                             "('Charlie', 21, 'C')")
conn.commit()

# Close the connection
conn.close()

Now that we have created the database, we can write a Python script to search the database using PySimpleGUI. We will create a simple GUI with an input field where the user can enter a student’s name and a search button to perform the search.

import PySimpleGUI as sg
import sqlite3

# Create a connection to the database
conn = sqlite3.connect('students.db')
cursor = conn.cursor()

# Define the layout of the GUI
layout = [[sg.Text('Enter student name:'), sg.InputText(key='name'), sg.Button('Search')],
          [sg.Text(size=(30, 1), key='output')]]

# Create the window
window = sg.Window('Search Student Database', layout)

# Event loop
while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    name = values['name']

    # Search the database for the student
    cursor.execute("SELECT * FROM students WHERE name=?", (name,))
    result = cursor.fetchone()

    if result is not None:
        output = f"Name: {result[1]}, Age: {result[2]}, Grade: {result[3]}"
    else:
        output = "Student not found"

    window['output'].update(output)

# Close the connection and window
conn.close()
window.close()

In this script, we first create a connection to the SQLite database and define the layout of the GUI using PySimpleGUI. We then create a window with an input field for the user to enter a student’s name and a search button to perform the search.

Inside the event loop, we retrieve the entered name from the input field and search the database for the student using a SQL query. If the student is found, we display their information in the output field; otherwise, we display a message indicating that the student was not found.

Finally, we close the connection to the database and the window when the user closes the window.

That’s it! You have now learned how to search an SQLite database using Python with PySimpleGUI. Feel free to modify the script to customize the GUI or add more advanced search functionality.

0 0 votes
Article Rating
7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@philluvschips3787
3 months ago

This only works for integers, if you want to amend the code to search for strings amend the SELECT,FROM WHERE line to read…
mycursor.execute("SELECT * FROM register WHERE record='"+search_record+"'")

@shenanigans004
3 months ago

i commented on your other video to make this why not just add this video to your playlist

@michaelcybulski4060
3 months ago

Can this be done referencing an excel? I know that’s probably not best practice but working with what I have

@a.m.m.elsayed2956
3 months ago

Did you press enter after entering phone number in sg.I(''SEARCH') to get results or it just uploaded automatically?

@domindomin1955
3 months ago

hello i have a question, is there a way to search in database with using string not int like you have used phone number and i want to use just name, but when im using name to search i get error 'no such column: (name)'

@computeradvantageslimited6518
3 months ago

Very useful. How can i download the underlying code??.Pls advise. Thanks.

@nimzing1
3 months ago

https://youtube.com/playlist?list=PLtkVrxK6azLKDcJs8OD4BE7BYU3njbIkp is the link to a complete playlist to learn the functionality of how to code a database using sqlite