-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py.bak
More file actions
38 lines (30 loc) · 1.11 KB
/
database.py.bak
File metadata and controls
38 lines (30 loc) · 1.11 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
import sqlite3
import os
db_path = os.path.join(os.path.dirname(__file__), 'data', 'inventory.db')
class Database:
def __init__(self):
self.conn = sqlite3.connect(db_path)
self.cursor = self.conn.cursor()
def execute_query(self, query, params=()):
"""Executes a query and commits changes."""
self.cursor.execute(query, params)
self.conn.commit()
def fetch_all(self, query, params=()):
"""Fetches all results for a query."""
self.cursor.execute(query, params)
return self.cursor.fetchall()
def fetch_one(self, query, params=()):
"""Fetches a single result for a query."""
self.cursor.execute(query, params)
return self.cursor.fetchone()
def close(self):
"""Closes the database connection."""
self.conn.close()
# Initialize the database
if __name__ == "__main__":
db = Database()
with open(os.path.join(os.path.dirname(__file__), 'schema.sql'), 'r') as schema_file:
db.cursor.executescript(schema_file.read())
db.conn.commit()
db.close()
print("Database initialized successfully.")