-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_populator.py
More file actions
58 lines (48 loc) · 1.48 KB
/
Copy pathdb_populator.py
File metadata and controls
58 lines (48 loc) · 1.48 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
import csv
import sqlite3
# Create or connect to a SQLite database
db_conn = sqlite3.connect("DB.db")
cursor = db_conn.cursor()
# Create the Orders table
cursor.execute("""
CREATE TABLE IF NOT EXISTS Orders (
order_id INTEGER PRIMARY KEY,
created_date TEXT,
merchant_id INTEGER
)
""")
# Create the Transactions table
cursor.execute("""
CREATE TABLE IF NOT EXISTS Transactions (
transaction_id INTEGER PRIMARY KEY,
created_date TEXT,
transaction_type TEXT,
order_id INTEGER,
transaction_status TEXT,
FOREIGN KEY (order_id) REFERENCES Orders(id)
)
""")
# Sample data for Orders
# Insert sample Orders data
with open('Orders.csv', 'r', newline='') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
cursor.execute("""
INSERT INTO Orders (order_id, created_date, merchant_id)
VALUES (?, ?, ?)
""",
(row['order_id'],row['created_date'],row['merchant_id']))
db_conn.commit()
# Insert sample Transactions data
with open('Transactions.csv', 'r', newline='') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
cursor.execute("""
INSERT INTO Transactions (created_date,order_id,transaction_type,transaction_status)
VALUES (?, ?, ?, ?)
""",
(row['created_date'],row['order_id'],row['transaction_type'],row['transaction_status']))
db_conn.commit()
# Close the database connection
db_conn.close()
print("Tables populated with sample data.")