import pandas as pd# For data manipulationimport sqlite3# For SQLite database interactionimport tkinter as tk# For GUI developmentfrom tkinterimport ttk, filedialog# For file dialog and progress bar
Pandas: A library( read_csv()) for reading CSV files and inserting data into SQLite.
2. Function: Load CSV to SQLite with Progress Tracking
defload_csv_to_sqlite():
# Prompt user to select a CSV file
file_path = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv"), ("All files", "*.*")])
if not file_path:
# Update status label if no file is selected
status_label.config(text="No file selected", fg="red")
return# Prompt user to select or create an SQLite database
db_path = filedialog.asksaveasfilename(defaultextension=".db", filetypes=[("SQLite files", "*.db")])
if not db_path:
# Update status label if no database is selected
status_label.config(text="No database selected", fg="red")
return# Connect to the SQLite database
my_conn = sqlite3.connect(db_path)
# Infer table name for storing CSV data
table_name = "data_table"# Load CSV in chunks
chunksize = 1000# Process 1000 rows at a time
total_rows = sum(1for _ inopen(file_path)) - 1
processed_rows = 0# Initialize processed rows counter# Configure the progress bar
progress_bar['maximum'] = total_rows
# Read and insert data chunk by chunktry:
for chunk in pd.read_csv(file_path, chunksize=chunksize):
chunk.to_sql(table_name, my_conn, if_exists='append', index=False)
processed_rows += len(chunk) # Update processed rows count
progress_bar['value'] = processed_rows
progress_bar.update_idletasks() # Update GUI
status_label.config(text=f"Loaded {processed_rows}/{total_rows} rows...")
except Exception as e:
status_label.config(text=f"Error: {e}", fg="red")
else:
status_label.config(text="Data loaded successfully!", fg="green")
finally:
my_conn.close()
File Dialog: Allows the user to select the CSV and SQLite files interactively.
Chunked Data Loading: Processes 1000 rows at a time for efficiency and progress tracking.
Progress Bar: Dynamically updates to show the loading progress.
3. Creating the Tkinter GUI
my_w = tk.Tk()
my_w.title("CSV to SQLite Loader")
my_w.geometry("500x200")
# Create a frame to organize the GUI components
frame = tk.Frame(my_w, padx=20, pady=20)
frame.pack(expand=True, fill="both")
# Title label
title_label = tk.Label(frame, text="CSV to SQLite Loader with Progress Tracking", font=("Arial", 14))
title_label.pack(pady=10)
# Button to start the loading process
load_button = tk.Button(frame, text="Load CSV to SQLite", command=load_csv_to_sqlite)
load_button.pack(pady=5)
# Progress bar
progress_bar = ttk.Progressbar(frame, orient="horizontal", length=400, mode="determinate")
progress_bar.pack(pady=10)
# Status label for displaying progress and error messages
status_label = tk.Label(frame, text="", font=("Arial", 10), fg="blue")
status_label.pack(pady=5)
# Start the Tkinter main loop
my_w.mainloop()
Tkinter Layout: Creates and arranges GUI components like buttons, progress bar, and labels.
Main Loop: Keeps the application running and responsive.
---
Key Features:
Interactive GUI: User-friendly interface for loading CSV files into SQLite.
Real-Time Progress Tracking: Progress bar dynamically updates during the loading process.
Chunked Data Handling: Efficiently processes large files in manageable chunks.
Excel to SQLite Loader with Progress Bar using #Tkinter & #Pandas
import pandas as pd
import sqlite3
import tkinter as tk
from tkinter import ttk, filedialog
# Function to load CSV and track progress
def load_csv_to_sqlite():
# Ask user to select a CSV file
file_path = filedialog.askopenfilename(filetypes=
[("CSV files", "*.csv"), ("All files", "*.*")])
if not file_path:
# Update status label if no file is selected
status_label.config(text="No file selected", fg="red")
return
# Ask user to select or create an SQLite database
db_path = filedialog.asksaveasfilename(defaultextension=".db",
filetypes=[("SQLite files", "*.db")])
if not db_path:
# Update status label if no database file is selected
status_label.config(text="No database selected", fg="red")
return
# Connect to the SQLite database
my_conn = sqlite3.connect(db_path) # Create connection object
# Infer table name for storing CSV data in the database
table_name = "data_table"
# Load CSV in chunks to handle large files
chunksize = 1000 # Process 1000 rows at a time
# Calculate total rows in the CSV (excluding header)
total_rows = sum(1 for _ in open(file_path)) - 1
processed_rows = 0 # Initialize counter for processed rows
# Configure progress bar maximum value
progress_bar['maximum'] = total_rows
# Read and insert data chunk by chunk
try:
for chunk in pd.read_csv(file_path, chunksize=chunksize):
# Append chunk data into the SQLite table
chunk.to_sql(table_name, my_conn, if_exists='append', index=False)
processed_rows += len(chunk) # Update processed rows count
# Update progress bar
progress_bar['value'] = processed_rows
progress_bar.update_idletasks() # Refresh the GUI to show progress
# Update status label with progress
status_label.config(text=f"Loaded {processed_rows}/{total_rows} rows...")
except Exception as e:
# Handle exceptions and update status label
status_label.config(text=f"Error: {e}", fg="red")
else:
# Update status label when data is loaded successfully
status_label.config(text="Data loaded successfully!", fg="green")
finally:
# Close the database connection
my_conn.close()
# Create the Tkinter GUI
my_w = tk.Tk() # Updated root to my_w
my_w.title("CSV to SQLite Loader") # Set the title of the GUI window
my_w.geometry("500x200") # Set the window size
# Add a frame to hold the GUI components
frame = tk.Frame(my_w, padx=20, pady=20)
frame.pack(expand=True, fill="both")
# Add a title label
title_label = tk.Label(frame,
text="CSV to SQLite Loader with Progress Tracking", font=("Arial", 14))
title_label.pack(pady=10)
# Add a button to load CSV and start the process
load_button = tk.Button(frame, text="Load CSV to SQLite", command=load_csv_to_sqlite)
load_button.pack(pady=5)
# Add a progress bar to show the loading progress
progress_bar = ttk.Progressbar(frame, orient="horizontal", length=400, mode="determinate")
progress_bar.pack(pady=10)
# Add a status label to display messages
status_label = tk.Label(frame, text="", font=("Arial", 10), fg="blue")
status_label.pack(pady=5)
# Run the Tkinter main loop
my_w.mainloop()
Creating extended CSV or Excel file of 10000 rows of data