-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_utils.py
51 lines (43 loc) · 1.54 KB
/
db_utils.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
# billing_system/db_utils.py
import mysql.connector as mysql
# Initialize the SQLite database
def init_db():
conn = mysql.connect(
host="localhost",
port="3306",
username="root",
password="",
database ='billing_system'
)
c = conn.cursor()
# Create the database if it doesn't exist
c.execute("CREATE DATABASE IF NOT EXISTS billing_system")
c.execute('''CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) UNIQUE,
password TEXT,
name TEXT,
address TEXT,
age INT,
role TEXT DEFAULT 'user'
)''')
c.execute('''CREATE TABLE IF NOT EXISTS products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) UNIQUE,
price FLOAT,
description TEXT,
category TEXT,
quantity INT DEFAULT 0
)''')
c.execute('''CREATE TABLE IF NOT EXISTS transactions (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
product_id INT,
quantity INT,
date DATETIME,
FOREIGN KEY(user_id) REFERENCES users(id),
FOREIGN KEY(product_id) REFERENCES products(id)
)''')
conn.commit()
conn.close()
# Other utility functions can be added as needed