-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
35 lines (26 loc) · 1012 Bytes
/
db.py
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
from flask import g
import sqlite3
DATABASE = 'database.db'
def get_db():
# Create a database connection in the current thread or reuse the existing one
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
return db
def create_tables():
db = get_db()
cursor = db.cursor()
# Create User table
db.execute('''CREATE TABLE IF NOT EXISTS users
(id INTEGER PRIMARY KEY, username TEXT NOT NULL, email TEXT NOT NULL, hash TEXT NOT NULL)''')
# Create Todo table
db.execute('''CREATE TABLE IF NOT EXISTS todos (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL,
task TEXT NOT NULL,
completed BOOLEAN NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- Add the created column with default value
FOREIGN KEY (user_id) REFERENCES users (id));''')
cursor.commit()
cursor.close()
#create_tables()