-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_database.py
More file actions
48 lines (42 loc) · 1.4 KB
/
setup_database.py
File metadata and controls
48 lines (42 loc) · 1.4 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
"""
Database Setup Script
Run this once to create the database and tables.
"""
import mysql.connector
# Connect to MySQL (without specifying database first)
conn = mysql.connector.connect(
host="localhost",
user="root",
password="NewPass123"
)
cursor = conn.cursor()
# Create database
cursor.execute("CREATE DATABASE IF NOT EXISTS inventory_app")
cursor.execute("USE inventory_app")
# Create products table
cursor.execute("""
CREATE TABLE IF NOT EXISTS products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description TEXT,
price DECIMAL(10, 2) NOT NULL,
quantity INT NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
# Check if products table is empty, then add sample data
cursor.execute("SELECT COUNT(*) FROM products")
if cursor.fetchone()[0] == 0:
cursor.execute("""
INSERT INTO products (name, description, price, quantity) VALUES
('Laptop', 'High performance laptop', 999.99, 10),
('Mouse', 'Wireless mouse', 29.99, 50),
('Keyboard', 'Mechanical keyboard', 79.99, 25)
""")
conn.commit()
cursor.close()
conn.close()
print("✅ Database 'inventory_app' setup completed successfully!")
print("✅ Table 'products' created!")
print("✅ Sample data inserted!")
print("\nYou can now run the app with: streamlit run app.py")