Skip to content

Commit b0584fa

Browse files
committed
Day 27 - Databases
1 parent 33c32f9 commit b0584fa

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed

Day 27 - Databases/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Day 27: Databases
2+
3+
## What You'll Learn:
4+
- SQLite database connections
5+
- Executing SQL queries from Python
6+
- Using SQLAlchemy ORM
7+
- CRUD (Create, Read, Update, Delete) operations
8+
- Database transactions and error handling
9+
10+
## Files:
11+
1. `sqlite_basics.py` - Raw SQL operations
12+
2. `crud_operations.py` - Basic CRUD functionality
13+
3. `orm_example.py` - SQLAlchemy ORM usage
14+
4. `database_schema.sql` - Table creation schema
15+
16+
## Exercises:
17+
1. Create a contact manager that:
18+
- Stores name/email/phone in SQLite
19+
- Allows search by name/phone
20+
- Exports contacts to CSV
21+
22+
2. Build an inventory system with:
23+
- Product table (name, price, quantity)
24+
- Daily sales tracking table
25+
- Low stock alerts
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sqlite3
2+
3+
class UserManager:
4+
def __init__(self, db_name='users.db'):
5+
self.conn = sqlite3.connect(db_name)
6+
self.cursor = self.conn.cursor()
7+
self._create_table()
8+
9+
def _create_table(self):
10+
self.cursor.execute('''CREATE TABLE IF NOT EXISTS users
11+
(id INTEGER PRIMARY KEY,
12+
name TEXT NOT NULL,
13+
age INTEGER)''')
14+
self.conn.commit()
15+
16+
def add_user(self, name, age):
17+
self.cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)",
18+
(name, age))
19+
self.conn.commit()
20+
21+
def get_users(self):
22+
return self.cursor.execute("SELECT * FROM users").fetchall()
23+
24+
def __del__(self):
25+
self.conn.close()
26+
27+
# Usage
28+
manager = UserManager()
29+
manager.add_user("Bob", 30)
30+
print("All users:", manager.get_users())
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Schema for blog application
2+
CREATE TABLE IF NOT EXISTS posts (
3+
id INTEGER PRIMARY KEY,
4+
title TEXT NOT NULL,
5+
content TEXT,
6+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
7+
);
8+
9+
CREATE TABLE IF NOT EXISTS comments (
10+
id INTEGER PRIMARY KEY,
11+
post_id INTEGER,
12+
author TEXT,
13+
comment TEXT,
14+
FOREIGN KEY(post_id) REFERENCES posts(id)
15+
);

Day 27 - Databases/orm_example.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from sqlalchemy import create_engine, Column, Integer, String
2+
from sqlalchemy.ext.declarative import declarative_base
3+
from sqlalchemy.orm import sessionmaker
4+
5+
Base = declarative_base()
6+
engine = create_engine('sqlite:///orm.db')
7+
8+
class Product(Base):
9+
__tablename__ = 'products'
10+
id = Column(Integer, primary_key=True)
11+
name = Column(String(50))
12+
price = Column(Integer)
13+
quantity = Column(Integer)
14+
15+
Base.metadata.create_all(engine)
16+
17+
Session = sessionmaker(bind=engine)
18+
session = Session()
19+
20+
# Add product
21+
new_product = Product(name="Laptop", price=999, quantity=5)
22+
session.add(new_product)
23+
session.commit()
24+
25+
# Query products
26+
print("All products:", session.query(Product).all())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SQLAlchemy==2.0.20
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sqlite3
2+
3+
# Create database connection
4+
conn = sqlite3.connect('mydatabase.db')
5+
cursor = conn.cursor()
6+
7+
# Create table
8+
cursor.execute('''CREATE TABLE IF NOT EXISTS users
9+
(id INTEGER PRIMARY KEY,
10+
name TEXT,
11+
email TEXT)''')
12+
13+
# Insert data
14+
cursor.execute("INSERT INTO users (name, email) VALUES (?, ?)",
15+
('Alice', 'alice@example.com'))
16+
17+
# Commit and close
18+
conn.commit()
19+
conn.close()

0 commit comments

Comments
 (0)