-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
44 lines (39 loc) · 1.58 KB
/
database.py
File metadata and controls
44 lines (39 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import sqlite3
from flask import g, current_app
DB_PATH = "library.db"
def get_db():
if "db" not in g:
g.db = sqlite3.connect(DB_PATH)
g.db.row_factory = sqlite3.Row
return g.db
def init_db():
db = sqlite3.connect(DB_PATH)
db.row_factory = sqlite3.Row
db.execute("""
CREATE TABLE IF NOT EXISTS books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author TEXT NOT NULL,
genre TEXT DEFAULT '',
year TEXT DEFAULT '',
status TEXT DEFAULT 'Available',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
# Seed sample data only when table is empty
count = db.execute("SELECT COUNT(*) FROM books").fetchone()[0]
if count == 0:
sample = [
("The Alchemist", "Paulo Coelho", "Fiction", "1988", "Available"),
("Clean Code", "Robert C. Martin", "Technology", "2008", "Issued"),
("Sapiens", "Yuval Noah Harari", "History", "2011", "Available"),
("Atomic Habits", "James Clear", "Self-Help", "2018", "Available"),
("The Great Gatsby", "F. Scott Fitzgerald","Classic", "1925", "Issued"),
("Introduction to Algorithms","Thomas Cormen", "Technology", "1990", "Available"),
]
db.executemany(
"INSERT INTO books (title, author, genre, year, status) VALUES (?, ?, ?, ?, ?)",
sample,
)
db.commit()
db.close()