-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_database_for_testing.py
53 lines (40 loc) · 1.38 KB
/
create_database_for_testing.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
48
49
50
51
52
53
import sqlite3
# Connect to a database (or create if it doesn't exist)
conn = sqlite3.connect('database.db') # This will create a file named 'my_database.db'
# Create a cursor object
cursor = conn.cursor()
# Create the 'Users' table
cursor.execute('''
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
master INTEGER REFERENCES users(id) CHECK (master != id)
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
price REAL NOT NULL,
bought INTEGER NOT NULL DEFAULT 0 CHECK (bought IN (0, 1)),
user_id INTEGER,
rating INTEGER CHECK (rating >= 1 AND rating <= 10),
FOREIGN KEY (user_id) REFERENCES Users(id)
)
''')
user_name = "Taha" # Example user name
cursor.execute("INSERT INTO users (name) VALUES (?)", (user_name,))
# Get the last inserted user's ID
user_id = cursor.lastrowid
# Insert products related to the user into the 'products' table
products = [
("Widget", 19.99, 0, 8),
("Gadget", 29.99, 1, 9)
]
for product in products:
cursor.execute("INSERT INTO products (name, price, bought, user_id, rating) VALUES (?, ?, ?, ?, ?)",
(product[0], product[1], product[2], user_id, product[3]))
# Commit the changes
conn.commit()
# Close the connection
conn.close()