Editor

Load Data into SQLite

Advanced · Write the code · from Build a Simple ETL Pipeline: Extract, Transform, Load

From the lesson: Step 5: Load into SQLite

The "L" in ETL means loading transformed data into its final destination. For us, that's a SQLite database. We'll create a table that matches our enriched record structure and insert all records in a batch.

Read this part of the lesson

Write a function load_to_database(records) that:

1. Creates an in-memory SQLite database.

2. Creates a customers table with columns: id (INTEGER PRIMARY KEY), name (TEXT), email (TEXT), amount (REAL), amount_tier (TEXT), email_domain (TEXT).

3. Inserts all records using executemany().

4. Commits and returns the connection.

Then query the database to verify: print the row count and all rows. Print "Loaded N records into database".

import sqlite3
def load_to_database(records):
# Create database, table, insert records
pass
# Sample enriched records
records = [
{'id': 1, 'name': 'Alice Johnson', 'email': 'alice@example.com', 'amount': 150.00, 'amount_tier': 'medium', 'email_domain': 'example.com'},
{'id': 2, 'name': 'Bob Smith', 'email': 'bob@company.org', 'amount': 230.50, 'amount_tier': 'medium', 'email_domain': 'company.org'},
{'id': 3, 'name': 'Charlie Brown', 'email': 'charlie@startup.io', 'amount': 75.00, 'amount_tier': 'low', 'email_domain': 'startup.io'},
{'id': 4, 'name': 'Diana Prince', 'email': 'diana@example.com', 'amount': 320.00, 'amount_tier': 'high', 'email_domain': 'example.com'},
]
conn = load_to_database(records)

More ETL Pipeline exercises

All Python exercises · 50 practice problems · Practical Projects quiz