forked from openai/swarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
188 lines (148 loc) · 4.36 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import sqlite3
# global connection
conn = None
def get_connection():
global conn
if conn is None:
conn = sqlite3.connect("application.db")
return conn
def create_database():
# Connect to a single SQLite database
conn = get_connection()
cursor = conn.cursor()
# Create Users table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS Users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
first_name TEXT,
last_name TEXT,
email TEXT UNIQUE,
phone TEXT
)
"""
)
# Create PurchaseHistory table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS PurchaseHistory (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
date_of_purchase TEXT,
item_id INTEGER,
amount REAL,
FOREIGN KEY (user_id) REFERENCES Users(user_id)
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS Products (
product_id INTEGER PRIMARY KEY,
product_name TEXT NOT NULL,
price REAL NOT NULL
);
"""
)
# Save (commit) the changes
conn.commit()
def add_user(user_id, first_name, last_name, email, phone):
conn = get_connection()
cursor = conn.cursor()
# Check if the user already exists
cursor.execute("SELECT * FROM Users WHERE user_id = ?", (user_id,))
if cursor.fetchone():
return
try:
cursor.execute(
"""
INSERT INTO Users (user_id, first_name, last_name, email, phone)
VALUES (?, ?, ?, ?, ?)
""",
(user_id, first_name, last_name, email, phone),
)
conn.commit()
except sqlite3.Error as e:
print(f"Database Error: {e}")
def add_purchase(user_id, date_of_purchase, item_id, amount):
conn = get_connection()
cursor = conn.cursor()
# Check if the purchase already exists
cursor.execute(
"""
SELECT * FROM PurchaseHistory
WHERE user_id = ? AND item_id = ? AND date_of_purchase = ?
""",
(user_id, item_id, date_of_purchase),
)
if cursor.fetchone():
# print(f"Purchase already exists for user_id {user_id} on {date_of_purchase} for item_id {item_id}.")
return
try:
cursor.execute(
"""
INSERT INTO PurchaseHistory (user_id, date_of_purchase, item_id, amount)
VALUES (?, ?, ?, ?)
""",
(user_id, date_of_purchase, item_id, amount),
)
conn.commit()
except sqlite3.Error as e:
print(f"Database Error: {e}")
def add_product(product_id, product_name, price):
conn = get_connection()
cursor = conn.cursor()
try:
cursor.execute(
"""
INSERT INTO Products (product_id, product_name, price)
VALUES (?, ?, ?);
""",
(product_id, product_name, price),
)
conn.commit()
except sqlite3.Error as e:
print(f"Database Error: {e}")
def close_connection():
global conn
if conn:
conn.close()
conn = None
def preview_table(table_name):
conn = sqlite3.connect("application.db") # Replace with your database name
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM {table_name} LIMIT 5;") # Limit to first 5 rows
rows = cursor.fetchall()
for row in rows:
print(row)
conn.close()
# Initialize and load database
def initialize_database():
global conn
# Initialize the database tables
create_database()
# Add some initial users
initial_users = [
(1, "Alice", "Smith", "[email protected]", "123-456-7890"),
(2, "Bob", "Johnson", "[email protected]", "234-567-8901"),
(3, "Sarah", "Brown", "[email protected]", "555-567-8901"),
# Add more initial users here
]
for user in initial_users:
add_user(*user)
# Add some initial purchases
initial_purchases = [
(1, "2024-01-01", 101, 99.99),
(2, "2023-12-25", 100, 39.99),
(3, "2023-11-14", 307, 49.99),
]
for purchase in initial_purchases:
add_purchase(*purchase)
initial_products = [
(7, "Hat", 19.99),
(8, "Wool socks", 29.99),
(9, "Shoes", 39.99),
]
for product in initial_products:
add_product(*product)