Quazim0t0 commited on
Commit
7aebe32
ยท
verified ยท
1 Parent(s): 5a55ea7

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +14 -76
database.py CHANGED
@@ -1,82 +1,20 @@
1
- from sqlalchemy import (
2
- create_engine,
3
- MetaData,
4
- Table,
5
- Column,
6
- String,
7
- Integer,
8
- Float,
9
- insert,
10
- text,
11
- )
12
 
13
- # Use a persistent SQLite database file
14
  engine = create_engine("sqlite:///database.db")
15
  metadata_obj = MetaData()
16
 
17
- # Define 'receipts' table
18
- receipts = Table(
19
- "receipts",
20
- metadata_obj,
21
- Column("receipt_id", Integer, primary_key=True),
22
- Column("customer_name", String(16)),
23
- Column("price", Float),
24
- Column("tip", Float),
25
- )
26
 
27
- # Create the table if it doesn't exist
28
- metadata_obj.create_all(engine)
 
 
 
 
 
29
 
30
- # Function to insert rows into the table
31
- def insert_rows_into_table(rows, table):
32
- with engine.begin() as connection:
33
- connection.execute(insert(table), rows)
34
-
35
- # Insert sample data if table is empty
36
- with engine.connect() as conn:
37
- result = conn.execute(text("SELECT COUNT(*) FROM receipts"))
38
- count = result.scalar()
39
- if count == 0:
40
- rows = [
41
- {"receipt_id": 1, "customer_name": "Ezekiel Frost", "price": 10.50, "tip": 2.00},
42
- {"receipt_id": 2, "customer_name": "Marla Dune", "price": 23.75, "tip": 4.50},
43
- {"receipt_id": 3, "customer_name": "Thorne Blackwood", "price": 55.20, "tip": 6.25},
44
- {"receipt_id": 4, "customer_name": "Selene Ashford", "price": 32.10, "tip": 3.50},
45
- {"receipt_id": 5, "customer_name": "Vesper Hawthorne", "price": 19.80, "tip": 2.75},
46
- {"receipt_id": 6, "customer_name": "Jaxon Raines", "price": 48.60, "tip": 5.00},
47
- {"receipt_id": 7, "customer_name": "Liora Vale", "price": 27.45, "tip": 3.00},
48
- {"receipt_id": 8, "customer_name": "Orion Finch", "price": 60.25, "tip": 7.50},
49
- {"receipt_id": 9, "customer_name": "Calista Morrigan", "price": 38.90, "tip": 4.25},
50
- {"receipt_id": 10, "customer_name": "Dorian Storm", "price": 50.75, "tip": 6.75},
51
- {"receipt_id": 11, "customer_name": "Lucian Crowe", "price": 11.90, "tip": 1.50},
52
- {"receipt_id": 12, "customer_name": "Eris Delacroix", "price": 29.95, "tip": 3.75},
53
- {"receipt_id": 13, "customer_name": "Zane Evermore", "price": 45.80, "tip": 5.50},
54
- {"receipt_id": 14, "customer_name": "Isolde Winter", "price": 31.20, "tip": 2.25},
55
- {"receipt_id": 15, "customer_name": "Galen Hallow", "price": 18.60, "tip": 2.00},
56
- {"receipt_id": 16, "customer_name": "Sable Whitmore", "price": 54.35, "tip": 6.00},
57
- {"receipt_id": 17, "customer_name": "Cassian Vex", "price": 28.10, "tip": 3.25},
58
- {"receipt_id": 18, "customer_name": "Selwyn Fox", "price": 61.45, "tip": 7.75},
59
- {"receipt_id": 19, "customer_name": "Thalia Rune", "price": 37.20, "tip": 4.00},
60
- {"receipt_id": 20, "customer_name": "Draven Nocturne", "price": 49.95, "tip": 6.25},
61
- {"receipt_id": 21, "customer_name": "Kieran Wolfe", "price": 12.75, "tip": 1.75},
62
- {"receipt_id": 22, "customer_name": "Ember Sterling", "price": 30.60, "tip": 3.50},
63
- {"receipt_id": 23, "customer_name": "Valko Nightshade", "price": 47.90, "tip": 5.75},
64
- {"receipt_id": 24, "customer_name": "Ronan Greaves", "price": 33.40, "tip": 3.00},
65
- {"receipt_id": 25, "customer_name": "Lyra Voss", "price": 20.50, "tip": 2.25},
66
- {"receipt_id": 26, "customer_name": "Caius Everdark", "price": 53.25, "tip": 6.50},
67
- {"receipt_id": 27, "customer_name": "Eowyn Vale", "price": 26.80, "tip": 2.75},
68
- {"receipt_id": 28, "customer_name": "Phoenix Ashen", "price": 59.30, "tip": 7.00},
69
- {"receipt_id": 29, "customer_name": "Leif Hawthorne", "price": 36.15, "tip": 3.75},
70
- {"receipt_id": 30, "customer_name": "Zephyr Sterling", "price": 48.75, "tip": 6.00},
71
- {"receipt_id": 31, "customer_name": "Solene Ardent", "price": 13.90, "tip": 1.25},
72
- {"receipt_id": 32, "customer_name": "Orpheus Dawn", "price": 31.95, "tip": 2.50},
73
- {"receipt_id": 33, "customer_name": "Nyx Holloway", "price": 46.85, "tip": 5.25},
74
- {"receipt_id": 34, "customer_name": "Rowan Nightshade", "price": 32.75, "tip": 3.00},
75
- {"receipt_id": 35, "customer_name": "Dante Crowley", "price": 22.50, "tip": 2.00},
76
- {"receipt_id": 36, "customer_name": "Sylas Vex", "price": 55.90, "tip": 6.75},
77
- {"receipt_id": 37, "customer_name": "Isabeau Winters", "price": 27.85, "tip": 3.25},
78
- {"receipt_id": 38, "customer_name": "Astra Everhart", "price": 62.10, "tip": 8.00},
79
- {"receipt_id": 39, "customer_name": "Tiberius Dusk", "price": 39.60, "tip": 4.50},
80
- {"receipt_id": 40, "customer_name": "Eldric Thorn", "price": 50.95, "tip": 6.25},
81
- ]
82
- insert_rows_into_table(rows, receipts)
 
1
+ from sqlalchemy import create_engine, MetaData, inspect
 
 
 
 
 
 
 
 
 
 
2
 
3
+ # Initialize database engine (Persistent SQLite)
4
  engine = create_engine("sqlite:///database.db")
5
  metadata_obj = MetaData()
6
 
7
+ # Function to check existing tables
8
+ def get_existing_tables():
9
+ inspector = inspect(engine)
10
+ return inspector.get_table_names()
 
 
 
 
 
11
 
12
+ # Function to initialize database if empty
13
+ def initialize_database():
14
+ tables = get_existing_tables()
15
+ if not tables:
16
+ print("No tables found in the database. Please upload an SQL file.")
17
+ else:
18
+ print(f"Database initialized with tables: {tables}")
19
 
20
+ initialize_database()