import tkinter as tk # Tkinter for GUI
from tkinter import filedialog, messagebox, simpledialog # Dialogs and alerts
import pandas as pd # Pandas for data manipulation
from sqlalchemy import create_engine # SQLAlchemy for SQLite connection
from sqlalchemy.exc import SQLAlchemyError # Handling database-related errors
df = None # To store the data loaded from the CSV file
engine = None # SQLite database engine for connecting and writing data
def load_csv():
"""Load a CSV file into a Pandas DataFrame."""
global df
file_path = filedialog.askopenfilename(
filetypes=[("CSV files", "*.csv"), ("All files", "*.*")]
)
if file_path:
try:
df = pd.read_csv(file_path)
text_area.delete("1.0", tk.END)
text_area.insert(tk.END, f"CSV loaded successfully! Preview:\n\n{df.head()}")
messagebox.showinfo("Success", "CSV file loaded successfully!")
except Exception as e:
messagebox.showerror("Error", f"Failed to load CSV: {e}")
def create_table():
"""Create an SQLite table based on the CSV structure and import the data."""
global df, engine
if df is None:
messagebox.showwarning("Warning", "No data loaded. Please load a CSV file first.")
return
db_path = filedialog.asksaveasfilename(
defaultextension=".db", filetypes=[("SQLite Database", "*.db")]
)
if not db_path:
return
try:
engine = create_engine(f"sqlite:///{db_path}")
table_name = simpledialog.askstring("Table Name", "Enter the name for the new table:")
if not table_name:
messagebox.showerror("Error", "Table name cannot be empty.")
return
df.to_sql(table_name, engine, if_exists="replace", index=False)
messagebox.showinfo("Success", f"Table 'table_name' created and data imported successfully.")
except SQLAlchemyError as e:
messagebox.showerror("Error", f"Failed to create table: {e}")
except Exception as e:
messagebox.showerror("Error", f"An unexpected error occurred: {e}")
my_w = tk.Tk()
my_w.title("Dynamic Schema Creation from CSV to SQLite")
my_w.geometry("800x600")
frame = tk.Frame(my_w)
frame.pack(pady=10)
btn_load_csv = tk.Button(
frame, text="Load CSV",
command=load_csv, width=20, bg="lightblue"
)
btn_load_csv.grid(row=0, column=0, padx=10)
btn_create_table = tk.Button(
frame, text="Create SQLite Table",
command=create_table, width=20, bg="lightgreen"
)
btn_create_table.grid(row=0, column=1, padx=10)
text_area = tk.Text(my_w, wrap="word", font=("Arial", 10))
text_area.pack(expand=True, fill="both", padx=10, pady=10)
my_w.mainloop()
# 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()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.