Spaces:
Sleeping
Sleeping
Create modules/database.py
Browse files- modules/database.py +32 -0
modules/database.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
DB_NAME = "menuvision.db"
|
| 5 |
+
|
| 6 |
+
def init_db():
|
| 7 |
+
conn = sqlite3.connect(DB_NAME)
|
| 8 |
+
c = conn.cursor()
|
| 9 |
+
# Create Tables
|
| 10 |
+
c.execute('''CREATE TABLE IF NOT EXISTS restaurants
|
| 11 |
+
(id INTEGER PRIMARY KEY, name TEXT, location TEXT, owner TEXT)''')
|
| 12 |
+
conn.commit()
|
| 13 |
+
conn.close()
|
| 14 |
+
|
| 15 |
+
def add_restaurant(name, location, owner):
|
| 16 |
+
try:
|
| 17 |
+
conn = sqlite3.connect(DB_NAME)
|
| 18 |
+
c = conn.cursor()
|
| 19 |
+
c.execute("INSERT INTO restaurants (name, location, owner) VALUES (?, ?, ?)", (name, location, owner))
|
| 20 |
+
conn.commit()
|
| 21 |
+
conn.close()
|
| 22 |
+
return f"Success: Restaurant '{name}' added!"
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"Error: {e}"
|
| 25 |
+
|
| 26 |
+
def search_restaurants(query):
|
| 27 |
+
conn = sqlite3.connect(DB_NAME)
|
| 28 |
+
# Simple SQL query to find matches
|
| 29 |
+
sql = f"SELECT name, location, owner FROM restaurants WHERE name LIKE '%{query}%' OR location LIKE '%{query}%'"
|
| 30 |
+
df = pd.read_sql_query(sql, conn)
|
| 31 |
+
conn.close()
|
| 32 |
+
return df
|