Create SQLite Tables Dynamically from CSV with a Tkinter-Powered GUI
This tutorial demonstrates how to create a Python application using Tkinter and Pandas for dynamically importing CSV files into an SQLite database. The application features a user-friendly GUI that enables users to load CSV files, preview the data, and dynamically create SQLite tables based on the file structure.
By leveraging SQLAlchemy, this tool ensures seamless integration with SQLite databases, making it easy to automate table creation and data import tasks. Additionally, robust error handling is implemented to manage corrupted files or invalid inputs, providing a reliable and efficient user experience.
This project is ideal for anyone looking to manage data transformations interactively, whether you're a beginner or an advanced Python developer exploring GUI applications.
1. Importing Libraries
import tkinter as tk # Tkinter for GUIfrom tkinter import filedialog, messagebox, simpledialog # Dialogs and alertsimport pandas as pd # Pandas for data manipulationfrom sqlalchemy import create_engine # SQLAlchemy for SQLite connectionfrom sqlalchemy.exc import SQLAlchemyError # Handling database-related errors
Tkinter: Used to create a GUI for file selection and user interaction.
Pandas: Handles the CSV data processing.
SQLAlchemy: Facilitates the SQLite database connection and table creation.
2. Global Variables
df = None# To store the data loaded from the CSV fileengine = None# SQLite database engine for connecting and writing data
df: Acts as a global DataFrame for loaded CSV data.
engine: Establishes and maintains the SQLite database connection.
Opens a file dialog to let the user select a CSV file.
Loads the CSV data into a Pandas DataFrame for processing.
Displays a preview of the loaded data and handles errors gracefully.
4. Function: Create SQLite Table
defcreate_table():
"""Create an SQLite table based on the CSV structure and import the data."""globaldf, engineifdfisNone:
messagebox.showwarning("Warning", "No data loaded. Please load a CSV file first.")
returndb_path = filedialog.asksaveasfilename(
defaultextension=".db", filetypes=[("SQLite Database", "*.db")]
)
if notdb_path:
returntry:
engine = create_engine(f"sqlite:///{db_path}")
table_name = simpledialog.askstring("Table Name", "Enter the name for the new table:")
if nottable_name:
messagebox.showerror("Error", "Table name cannot be empty.")
returndf.to_sql(table_name, engine, if_exists="replace", index=False)
messagebox.showinfo("Success", f"Table 'table_name' created and data imported successfully.")
except SQLAlchemyError ase:
messagebox.showerror("Error", f"Failed to create table: {e}")
except Exception ase:
messagebox.showerror("Error", f"An unexpected error occurred: {e}")
Prompts the user to specify the SQLite database file and table name.
Dynamically creates a table based on the structure of the loaded CSV file.
Imports the CSV data into the newly created SQLite table.
Handles database errors and unexpected issues gracefully.
Sets up the Tkinter GUI window with a user-friendly layout.
Includes buttons for loading CSV files and creating SQLite tables dynamically.
A text area is provided to display the preview and status messages.
# Importing required libraries
import tkinter as tk # For creating the GUI
from tkinter import filedialog, messagebox, simpledialog # For dialogs and alerts
import pandas as pd # For data manipulation and processing
from sqlalchemy import create_engine # For connecting to SQLite database
from sqlalchemy.exc import SQLAlchemyError # For handling database-related errors
# Global variables
df = None # To store the data loaded from the CSV file
engine = None # SQLite database engine for connecting and writing data
# Function to select a CSV file and load it into a DataFrame
def load_csv():
"""
Load a CSV file into a Pandas DataFrame.
Opens a file dialog for the user to select a CSV file.
Displays a preview of the loaded data in the text area.
"""
global df
# Open a file dialog to select a CSV file
file_path = filedialog.askopenfilename(
filetypes=[("CSV files", "*.csv"), ("All files", "*.*")]
)
if file_path: # Check if the user selected a file
try:
# Load the CSV file into a Pandas DataFrame
df = pd.read_csv(file_path)
# Clear the text area and display a preview of the loaded data
text_area.delete("1.0", tk.END)
text_area.insert(tk.END, f"CSV loaded successfully! Preview:\n\n{df.head()}")
# Show a success message
messagebox.showinfo("Success", "CSV file loaded successfully!")
except Exception as e: # Catch errors while loading the CSV
messagebox.showerror("Error", f"Failed to load CSV: {e}")
# Function to create an SQLite table dynamically and import data
def create_table():
"""
Create an SQLite table based on the CSV structure and import the data.
Prompts the user to specify the database file and table name.
"""
global df, engine
if df is None: # Check if data is loaded
messagebox.showwarning("Warning", "No data loaded. Please load a CSV file first.")
return
# Open a file dialog for the user to specify the SQLite database file
db_path = filedialog.asksaveasfilename(
defaultextension=".db", filetypes=[("SQLite Database", "*.db")]
)
if not db_path: # Exit if the user cancels the dialog
return
try:
# Create an SQLite engine for the specified database
engine = create_engine(f"sqlite:///{db_path}")
# Prompt the user to enter a name for the new SQLite table
table_name = simpledialog.askstring("Table Name", "Enter the name for the new table:")
if not table_name: # Validate that a table name was entered
messagebox.showerror("Error", "Table name cannot be empty.")
return
# Write the DataFrame to the SQLite database as the specified table
df.to_sql(table_name, engine, if_exists="replace", index=False)
# Show a success message
messagebox.showinfo("Success", f"Table '{table_name}' created and data imported successfully.")
except SQLAlchemyError as e: # Catch database-related errors
messagebox.showerror("Error", f"Failed to create table: {str(e)}")
except Exception as e: # Catch other unexpected errors
messagebox.showerror("Error", f"An unexpected error occurred: {e}")
# Tkinter GUI Setup
my_w = tk.Tk() # Create the main application window
my_w.title("Dynamic Schema Creation from CSV to SQLite") # Set the window title
my_w.geometry("800x600") # Set the dimensions of the window
# Frame for buttons
frame = tk.Frame(my_w) # Create a frame to organize buttons
frame.pack(pady=10) # Add some padding above and below the frame
# Button to load a CSV file
btn_load_csv = tk.Button(
frame, text="Load CSV", # Button label
command=load_csv, # Function to execute on button click
width=20, # Width of the button
bg="lightblue" # Background color of the button
)
btn_load_csv.grid(row=0, column=0, padx=10) # Position the button with padding
# Button to create SQLite table
btn_create_table = tk.Button(
frame, text="Create SQLite Table", # Button label
command=create_table, # Function to execute on button click
width=20, # Width of the button
bg="lightgreen" # Background color of the button
)
btn_create_table.grid(row=0, column=1, padx=10) # Position the button with padding
# Text area to display messages and a preview of the data
text_area = tk.Text(my_w, wrap="word", font=("Arial", 10)) # Create a text area widget
text_area.pack(expand=True, fill="both", padx=10, pady=10) # Make it fill the available space
# Start the Tkinter main event loop
my_w.mainloop()