Inserting Data into Database from Python
GUI
Handout (Approx. 5 Pages)
1. Introduction
Graphical User Interfaces (GUIs) allow users to interact with applications easily without
typing commands. When combined with Python and MySQL, GUIs enable users to enter
data through forms and store it directly into a database. This approach is widely used in
student registration systems, employee management systems, inventory applications, and
many business solutions.
This handout explains how to design a simple Python GUI and insert user-entered data into
a MySQL database securely and efficiently.
2. Technologies Used
To insert data into a MySQL database from a Python GUI, the following technologies are
commonly used:
• Python – Application logic
• MySQL Server – Database storage
• MySQL Connector for Python – Database connectivity
• Tkinter – Python’s standard GUI library
Tkinter is preferred in academic environments because it is simple, lightweight, and
included with Python.
3. Prerequisites
Before developing the GUI application, ensure that:
• Python 3.x is installed
• MySQL Server is installed and running
• mysql-connector-python is installed
• A database and table already exist
3.1 Sample Database and Table
CREATE DATABASE school_db;
USE school_db;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
age INT,
department VARCHAR(50)
);
4. Basic GUI Design Using Tkinter
A simple form-based GUI includes labels, entry fields, and a submit button.
import tkinter as tk
from tkinter import messagebox
import [Link]
Create the main window:
root = [Link]()
[Link]("Student Registration")
[Link]("300x250")
5. Connecting GUI to MySQL Database
5.1 Database Connection Function
def connect_db():
return [Link](
host="localhost",
user="root",
password="password",
database="school_db"
)
This function establishes a reusable connection to the MySQL database.
6. Inserting Data from GUI to Database
6.1 Insert Function Logic
def insert_data():
name = entry_name.get()
age = entry_age.get()
dept = entry_dept.get()
if name == "" or age == "" or dept == "":
[Link]("Error", "All fields are required")
return
try:
conn = connect_db()
cursor = [Link]()
sql = "INSERT INTO students (name, age, department) VALUES (%s, %s, %s)"
values = (name, age, dept)
[Link](sql, values)
[Link]()
[Link]("Success", "Data inserted successfully")
[Link]()
[Link]()
except [Link] as err:
[Link]("Database Error", str(err))
7. Creating GUI Form Components
[Link](root, text="Name").pack()
entry_name = [Link](root)
entry_name.pack()
[Link](root, text="Age").pack()
entry_age = [Link](root)
entry_age.pack()
[Link](root, text="Department").pack()
entry_dept = [Link](root)
entry_dept.pack()
[Link](root, text="Submit", command=insert_data).pack(pady=10)
Run the GUI:
[Link]()
8. Data Validation and Error Handling
Good GUI applications validate user input:
• Check for empty fields
• Ensure numeric fields contain numbers
• Catch database errors using try–except blocks
This prevents invalid or incomplete data from being stored.
9. Security and Best Practices
• Use parameterized queries to prevent SQL injection
• Do not hardcode credentials in real applications
• Close database connections properly
• Use form validation for better data quality
10. Practical Applications
Python GUI applications with MySQL are used in:
• Student information systems
• Employee registration systems
• Library management systems
• Inventory and sales systems
• Small business data entry tools