Sqlite
Source: 11-Working With Databases/11.1-sqlite.ipynb
Start here — no coding background needed
What you will learn
Store data in SQL tables — query with SELECT.
In simple words
Database = permanent table. SQL language asks questions: "give all students with marks > 80".
Store data in organized tables — like a digital filing cabinet.
Easy example — run this first. Change values and press Run again.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Reference notes (from full bootcamp)
Optional — deeper detail for when you are ready
SQL And SQLite
SQL (Structured Query Language) is a standard language for managing and manipulating relational databases. SQLite is a self-contained, serverless, and zero-configuration database engine that is widely used for embedded database systems. In this lesson, we will cover the basics of SQL and SQLite, including creating databases, tables, and performing various SQL operations.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
## Connect to an SQLite database
connection=sqlite3.connect('example.db')
connectionBrowser practice only — full example needs Python on your computer (files, Flask, threads, etc.).
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): <sqlite3.Cursor at 0x200619658c0>
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): (1, 'Anshul', 32, 'Data Scientist') (2, 'Bob', 25, 'Engineering') (3, 'Charlie', 35, 'Finance')
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): (1, 'Anshul', 34, 'Data Scientist') (2, 'Bob', 25, 'Engineering') (3, 'Charlie', 35, 'Finance')
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): (1, 'Anshul', 34, 'Data Scientist') (3, 'Charlie', 35, 'Finance')
Runs in your browser via Pyodide — no server. First run may take a few seconds.
## Working Wwith Sales Data
# Connect to an SQLite database
connection = sqlite3.connect('sales_data.db')
cursor = connection.cursor()
# Create a table for sales data
cursor.execute('''
CREATE TABLE IF NOT EXISTS sales (
id INTEGER PRIMARY KEY,
date TEXT NOT NULL,
product TEXT NOT NULL,
sales INTEGER,
region TEXT
)
''')
# Insert data into the sales table
sales_data = [
('2023-01-01', 'Product1', 100, 'North'),
('2023-01-02', 'Product2', 200, 'South'),
('2023-01-03', 'Product1', 150, 'East'),
('2023-01-04', 'Product3', 250, 'West'),
('2023-01-05', 'Product2', 300, 'North')
]
cursor.executemany('''
Insert into sales(date,product,sales,region)
values(?,?,?,?)
''',sales_data)
connection.commit()Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).
Expected (from notebook): (1, '2023-01-01', 'Product1', 100, 'North') (2, '2023-01-02', 'Product2', 200, 'South') (3, '2023-01-03', 'Product1', 150, 'East') (4, '2023-01-04', 'Product3', 250, 'West') (5, '2023-01-05', 'Product2', 300, 'North')
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Practice test — try yourself
Write code, press Check. Wrong answer shows the correct code to copy & run.
You learned "Sqlite". Use print() to show: Done: Sqlite
Hint: Use one print() with the exact text.