-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.py
More file actions
77 lines (61 loc) · 2.61 KB
/
db.py
File metadata and controls
77 lines (61 loc) · 2.61 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import sqlite3
import os
# Database file path
DB_PATH = 'database.db'
UPLOAD_FOLDER = 'static/uploads/' # Ensure videos are stored correctly
def create_tables():
"""Initialize database and create necessary tables if they do not exist."""
# Ensure the database exists
if not os.path.exists(DB_PATH):
open(DB_PATH, 'w').close()
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
# Create `users` table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
);
''')
# Create `videos` table
cursor.execute('''
CREATE TABLE IF NOT EXISTS videos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
title TEXT NOT NULL,
video_path TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
''')
conn.commit()
print(f"✅ Database '{DB_PATH}' initialized and tables created.")
def insert_sample_data():
"""Insert a sample user and video for testing if the tables are empty."""
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
# Check if there's any user data already
cursor.execute("SELECT COUNT(*) FROM users;")
user_count = cursor.fetchone()[0]
if user_count == 0:
cursor.execute("INSERT INTO users (username, password) VALUES (?, ?);", ("test_user", "password123"))
conn.commit()
print("✅ Sample user added: test_user")
# Get the user ID
cursor.execute("SELECT id FROM users WHERE username = ?;", ("test_user",))
user_id = cursor.fetchone()[0]
# Check if video data exists
cursor.execute("SELECT COUNT(*) FROM videos;")
video_count = cursor.fetchone()[0]
if video_count == 0:
sample_video_path = os.path.join(UPLOAD_FOLDER, f"{user_id}/test_video.mp4")
# Ensure the directory exists
user_video_folder = os.path.dirname(sample_video_path)
os.makedirs(user_video_folder, exist_ok=True)
cursor.execute("INSERT INTO videos (user_id, title, video_path) VALUES (?, ?, ?);",
(user_id, "Sample Video", sample_video_path))
conn.commit()
print(f"✅ Sample video added: {sample_video_path}")
if __name__ == "__main__":
create_tables()
insert_sample_data()