Part 6 of the Introduction to PySimpleGUI series discusses how to integrate SQLite databases into your PySimpleGUI applications.

Posted by


Welcome to Part 6 of our Intro to PySimpleGUI tutorial series! In this tutorial, we will learn how to integrate SQLite databases with PySimpleGUI. SQLite is a lightweight database engine that allows you to store and manage data in a structured format. By integrating SQLite databases with PySimpleGUI, you can create powerful applications that can store and retrieve data easily.

To get started, make sure you have SQLite installed on your system. You can download SQLite from the official website (https://www.sqlite.org/download.html) and follow the installation instructions.

First, let’s create a simple GUI application using PySimpleGUI that allows users to add data to a SQLite database. We will create a form with input fields for name, email, and phone number. When the user submits the form, the data will be added to the SQLite database.

import PySimpleGUI as sg
import sqlite3

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

# Create a table in the database
c.execute('''CREATE TABLE IF NOT EXISTS users
             (name TEXT, email TEXT, phone TEXT)''')
conn.commit()

layout = [
    [sg.Text('Name:'), sg.InputText(key='name')],
    [sg.Text('Email:'), sg.InputText(key='email')],
    [sg.Text('Phone:'), sg.InputText(key='phone')],
    [sg.Button('Submit')]
]

window = sg.Window('Add User', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    if event == 'Submit':
        # Insert data into the database
        c.execute('INSERT INTO users VALUES (?, ?, ?)', (values['name'], values['email'], values['phone']))
        conn.commit()
        sg.popup('User added successfully!')

conn.close()

In this code, we first create a connection to the SQLite database using the sqlite3 module. We then create a table called users with columns for name, email, and phone number. The user can enter their data in the input fields and click the Submit button to add their information to the database.

Next, let’s create a GUI application that allows users to view the data stored in the SQLite database. We will display the data in a table format.

layout = [
    [sg.Table(values=[], headings=['Name', 'Email', 'Phone'], key='table', auto_size_columns=False)],
]

window = sg.Window('View Users', layout)

# Retrieve data from the database and display it in the table
data = c.execute('SELECT * FROM users').fetchall()
window['table'].update(values=data)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break

conn.close()

In this code, we create a new window with a Table element that will display the data from the users table in the SQLite database. We retrieve the data using a SQL query and update the Table element with the fetched data.

By integrating SQLite databases with PySimpleGUI, you can create powerful applications that can store and manage data efficiently. Experiment with different SQL queries and GUI layouts to create custom applications that fit your needs. I hope this tutorial has been helpful in getting you started with SQLite databases in PySimpleGUI. Happy coding!

0 0 votes
Article Rating

Leave a Reply

9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@EbesohBrooklyn
22 days ago

Why is it so small

@busycow8334
22 days ago

Thanks, Helped me alot

@jok3xxxd
22 days ago

This video helped me out a lot, but do you have any way of deleting rows from the table, with a button in the table window?

@hasanaqbayli149
22 days ago

Great Course!!! Do you have this code in one file instead of separate files ?
By the way do you have any courses somewhere in Udemy or Coursera ? Very well explained courses with good English and Programming language;

@nicoloscalzotto1108
22 days ago

I have watched all your videos about pysimplegui. Thanks to your guide I managed to create a CURD not too dissimilar to the one in this video. I would like to implement the ability to select a row from the table and update or delete it. I tried searching in the docs, on github and on stackoverflow but couldn't find a way to get my code to work. Do you have any advice? Thanks in advance.

@cristhianjfonseca2890
22 days ago

Thank You!! If I need to make an .exe for my program (with pyinstaller), how do I put together so many files? (The ones you created for each thing (sql, main..)?

@killerweedboy
22 days ago

You are a great teacher! Very concise. Thank you !!

@paulo.galvao
22 days ago

you can install a sqlite extension to view databases on vscode

@nimzing1
22 days ago

Just what I have been searching for 4 a long time! I just subscribed…I hope to get some tips here

9
0
Would love your thoughts, please comment.x
()
x