In Python GUI development, how do I store results in a database?

In Python GUI development, storing results in a database is a common task that can be achieved using libraries such as SQLite3 or SQLAlchemy. Here is a simple example demonstrating how to insert data into a SQLite database using a Python GUI application.

import sqlite3 from tkinter import * # Create a GUI application root = Tk() root.title("Database Example") # Function to insert data into the database def insert_data(): conn = sqlite3.connect('example.db') cursor = conn.cursor() # Create a table cursor.execute('''CREATE TABLE IF NOT EXISTS results (id INTEGER PRIMARY KEY, name TEXT)''') # Insert a new record name = entry.get() cursor.execute('INSERT INTO results (name) VALUES (?)', (name,)) conn.commit() cursor.close() conn.close() # Clear entry field entry.delete(0, END) # GUI layout entry = Entry(root) entry.pack() button = Button(root, text="Insert", command=insert_data) button.pack() root.mainloop()

python gui sqlite database Tkinter data storage