Skip to content

Instantly share code, notes, and snippets.

@bruskiza
Last active June 4, 2025 20:40
Show Gist options
  • Save bruskiza/c57ee93c254851474d1548e0c9c3d2da to your computer and use it in GitHub Desktop.
Save bruskiza/c57ee93c254851474d1548e0c9c3d2da to your computer and use it in GitHub Desktop.
loader.py
import pandas as pd
from sqlalchemy import create_engine
excel_file_path = 'data.xlsx' # Path to your Excel file
db_name = 'data.db' # Name of the SQLite database
table_name = 'data_table' # Name of the table to create in the database
# 1. Read the Excel file into a pandas DataFrame
df = pd.read_excel(excel_file_path)
print("Excel file read successfully.")
# 2. Create a SQLAlchemy engine for SQLite
# The 'sqlite:///' prefix indicates a local SQLite database
engine = create_engine(f'sqlite:///{db_name}')
print(f"Connecting to SQLite database: {db_name}")
# 3. Write the DataFrame to the SQLite database
# if_exists='replace' will drop the table if it exists and recreate it
# if_exists='append' will add new rows to the existing table
# if_exists='fail' will raise a ValueError if the table exists
df.to_sql(table_name, engine, if_exists='replace', index=False)
print(f"Data successfully written to table '{table_name}' in '{db_name}'.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment