-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
123 lines (109 loc) · 3.5 KB
/
Copy pathdatabase.py
File metadata and controls
123 lines (109 loc) · 3.5 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
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
import sqlite3
DB_NAME = "hotel.db"
def init_db():
"""
Creates the database tables if they don't exist.
"""
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
# 1. Users Table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
phone_number TEXT UNIQUE,
joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# 2. Orders Table
cursor.execute('''
CREATE TABLE IF NOT EXISTS orders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
phone_number TEXT,
order_details TEXT,
total_price REAL,
payment_id TEXT,
status TEXT DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# 3. Menu Table
cursor.execute('''
CREATE TABLE IF NOT EXISTS menu (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE,
price REAL
)
''')
# Check if menu is empty, if so, add default items
cursor.execute('SELECT count(*) FROM menu')
if cursor.fetchone()[0] == 0:
default_items = [
("Chicken Burger", 120),
("Veg Pizza", 200),
("Coke", 40),
("Fries", 80)
]
cursor.executemany("INSERT INTO menu (name, price) VALUES (?, ?)", default_items)
print("✅ Default menu created.")
conn.commit()
conn.close()
# --- USER FUNCTIONS ---
def add_user(phone_number):
"""Adds a new user if they don't exist."""
conn = sqlite3.connect(DB_NAME)
try:
with conn:
conn.execute("INSERT OR IGNORE INTO users (phone_number) VALUES (?)", (phone_number,))
finally:
conn.close()
# --- ORDER FUNCTIONS ---
def add_order(phone_number, order_details, total_price, payment_id):
"""Saves a new order."""
conn = sqlite3.connect(DB_NAME)
try:
with conn:
conn.execute('''
INSERT INTO orders (phone_number, order_details, total_price, payment_id)
VALUES (?, ?, ?, ?)
''', (phone_number, order_details, total_price, payment_id))
finally:
conn.close()
# --- MENU FUNCTIONS (AI Helper) ---
def get_menu_string():
"""Returns the menu as a formatted string for the AI."""
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
cursor.execute("SELECT name, price FROM menu")
items = cursor.fetchall()
conn.close()
# Format: "Item : ₹Price\n"
menu_text = "\n".join([f"{item[0]} : ₹{item[1]}" for item in items])
return menu_text
# --- MENU MANAGEMENT (Dashboard Helpers) ---
def add_menu_item(name, price):
"""Adds a new item to the database."""
conn = sqlite3.connect(DB_NAME)
try:
with conn:
conn.execute("INSERT INTO menu (name, price) VALUES (?, ?)", (name, price))
return True
except sqlite3.IntegrityError:
return False # Item already exists
finally:
conn.close()
def delete_menu_item(item_name):
"""Removes an item from the database."""
conn = sqlite3.connect(DB_NAME)
try:
with conn:
conn.execute("DELETE FROM menu WHERE name = ?", (item_name,))
finally:
conn.close()
def get_raw_menu():
"""Returns the menu as a list of tuples for the GUI."""
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
cursor.execute("SELECT name, price FROM menu")
items = cursor.fetchall()
conn.close()
return items