Spaces:
Configuration error
Configuration error
Download create_db.py from Huzaifa1313/sql-agent-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/spaces/Huzaifa1313/sql-agent-backend/resolve/main/create_db.py
- Command line
-
hf download hf://spaces/Huzaifa1313/sql-agent-backend/create_db.py
-
curl -L -o create_db.py https://huggingface.co/spaces/Huzaifa1313/sql-agent-backend/resolve/main/create_db.py
1.22 kB
| """ | |
| Run this once to create the default sample database. | |
| Usage: python create_db.py | |
| """ | |
| import sqlite3 | |
| import os | |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| DB_FOLDER = os.path.join(BASE_DIR, "database") | |
| DB_PATH = os.path.join(DB_FOLDER, "company.db") | |
| os.makedirs(DB_FOLDER, exist_ok=True) | |
| conn = sqlite3.connect(DB_PATH) | |
| cursor = conn.cursor() | |
| cursor.execute(""" | |
| CREATE TABLE IF NOT EXISTS customers( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| name TEXT NOT NULL, | |
| age INTEGER, | |
| city TEXT, | |
| email TEXT | |
| ) | |
| """) | |
| cursor.execute("SELECT COUNT(*) FROM customers") | |
| count = cursor.fetchone()[0] | |
| if count == 0: | |
| customers = [ | |
| ("Ali", 23, "Lahore", "ali@gmail.com"), | |
| ("Ahmed", 25, "Islamabad", "ahmed@gmail.com"), | |
| ("Sara", 22, "Karachi", "sara@gmail.com"), | |
| ("Usman", 27, "Lahore", "usman@gmail.com"), | |
| ("Ayesha", 24, "Faisalabad", "ayesha@gmail.com"), | |
| ] | |
| cursor.executemany( | |
| "INSERT INTO customers(name, age, city, email) VALUES (?, ?, ?, ?)", | |
| customers, | |
| ) | |
| print("✅ Sample data inserted.") | |
| else: | |
| print("ℹ️ Data already exists, skipping insert.") | |
| conn.commit() | |
| conn.close() | |
| print(f"✅ Database ready at: {DB_PATH}") | |