-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.python
More file actions
57 lines (48 loc) · 1.4 KB
/
Copy pathtempCodeRunnerFile.python
File metadata and controls
57 lines (48 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
49
50
51
52
53
54
55
56
57
import sqlite3
import os
# Path to your database
DB_FILE = r"D:\Inventory\inventory.db"
def init_db():
"""Initialize the database and tables if they don't exist"""
# Create folder if it doesn't exist
os.makedirs(os.path.dirname(DB_FILE), exist_ok=True)
# Connect to SQLite (will create file if not exists)
conn = sqlite3.connect(DB_FILE)
cur = conn.cursor()
# Create inventory table
cur.execute("""
CREATE TABLE IF NOT EXISTS inventory (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
quantity INTEGER NOT NULL,
price REAL NOT NULL
)
""")
# Create purchase_list table
cur.execute("""
CREATE TABLE IF NOT EXISTS purchase_list (
id INTEGER,
name TEXT NOT NULL,
quantity INTEGER NOT NULL,
price REAL NOT NULL
)
""")
# Insert sample inventory data (replace or add)
sample_items = [
(101, 'Item A', 50, 10.0),
(102, 'Item B', 30, 15.0),
(103, 'Item C', 20, 8.0),
(104, 'Item D', 40, 15.0),
(105, 'Item E', 60, 15.0),
(106, 'Item F', 10, 15.0),
]
for item in sample_items:
cur.execute("""
INSERT OR REPLACE INTO inventory (id, name, quantity, price)
VALUES (?, ?, ?, ?)
""", item)
conn.commit()
conn.close()
print(f"Database initialized at {DB_FILE}!")
if __name__ == "__main__":
init_db()