-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.py
47 lines (40 loc) · 1.65 KB
/
database.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
36
37
38
39
40
41
42
43
44
45
46
47
import sqlite3
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
# Create Users table with constraints specified at the time of table creation
cursor.execute('''CREATE TABLE IF NOT EXISTS Users (
UserID INT PRIMARY KEY,
Email VARCHAR(100) NOT NULL,
Password VARCHAR(100) NOT NULL CHECK (LENGTH(Password) >= 5));''')
# Create Folders table with foreign key constraints specified at creation
cursor.execute('''CREATE TABLE IF NOT EXISTS Folders (
FolderID INT PRIMARY KEY,
UserID INT,
FolderName VARCHAR(100),
IsPublic BOOLEAN,
FOREIGN KEY (UserID) REFERENCES Users(UserID));''')
# Create Flashcards table with foreign key constraints specified at creation
cursor.execute('''CREATE TABLE IF NOT EXISTS Flashcards (
FlashcardID INT PRIMARY KEY,
FolderID INT,
Question TEXT,
Answer TEXT,
FOREIGN KEY (FolderID) REFERENCES Folders(FolderID) ON DELETE CASCADE);''')
# Create FolderAccess table with foreign key constraints specified at creation
cursor.execute('''CREATE TABLE IF NOT EXISTS FolderAccess (
UserID INT,
FolderID INT,
PRIMARY KEY (UserID, FolderID),
FOREIGN KEY (UserID) REFERENCES Users(UserID) ON DELETE CASCADE,
FOREIGN KEY (FolderID) REFERENCES Folders(FolderID) ON DELETE CASCADE);''')
# Create Sessions table with foreign key constraints specified at creation
cursor.execute('''CREATE TABLE IF NOT EXISTS Sessions (
SessionID INT PRIMARY KEY,
UserID INT,
Token VARCHAR(255),
ExpiryDateTime DATETIME,
FOREIGN KEY (UserID) REFERENCES Users(UserID) ON DELETE CASCADE);''')
# Commit changes to the database and close the connections
conn.commit()
cursor.close()
conn.close()