Spaces:
Sleeping
Sleeping
File size: 2,694 Bytes
6f373ab | 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 | import sqlite3
def init_db():
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
role TEXT NOT NULL CHECK (role IN ('admin', 'student'))
)''')
c.execute('''CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
)''')
c.execute('''CREATE TABLE IF NOT EXISTS skills (
id INTEGER PRIMARY KEY AUTOINCREMENT,
student_id INTEGER NOT NULL,
name TEXT NOT NULL,
score INTEGER DEFAULT 0,
url TEXT NOT NULL,
FOREIGN KEY (student_id) REFERENCES students(id)
)''')
conn.commit()
conn.close()
def seed_db():
conn = sqlite3.connect('data.db')
c = conn.cursor()
# Check if the users table is empty
c.execute('SELECT COUNT(*) FROM users')
if c.fetchone()[0] == 0:
# Insert initial admin user
c.execute("INSERT INTO users (username, password, role) VALUES (?, ?, ?)",
('admin', 'admin_password', 'admin'))
# Insert initial student user
c.execute("INSERT INTO users (username, password, role) VALUES (?, ?, ?)",
('student', 'student_password', 'student'))
# Check if the students table is empty
c.execute('SELECT COUNT(*) FROM students')
if c.fetchone()[0] == 0:
# Insert initial students
c.execute("INSERT INTO students (name) VALUES (?)", ('Student A',))
c.execute("INSERT INTO students (name) VALUES (?)", ('Student B',))
# Check if the skills table is empty
c.execute('SELECT COUNT(*) FROM skills')
if c.fetchone()[0] == 0:
# Insert initial skills for the first student
c.execute("INSERT INTO skills (student_id, name, url) VALUES (?, ?, ?)",
(1, 'Math', 'http://example.com/math'))
c.execute("INSERT INTO skills (student_id, name, url) VALUES (?, ?, ?)",
(1, 'Science', 'http://example.com/science'))
# Insert initial skills for the second student
c.execute("INSERT INTO skills (student_id, name, url) VALUES (?, ?, ?)",
(2, 'English', 'http://example.com/english'))
conn.commit()
conn.close()
print("Database seeded successfully.")
if __name__ == '__main__':
init_db() # Initialize the database
seed_db() # Seed the database with initial data
|