Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
122 lines (97 loc) · 3.98 KB
/
Copy pathdatabase.py
File metadata and controls
122 lines (97 loc) · 3.98 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# DATABASE FILE (database.py)
import sqlite3
# Function to connect to the SQLite database
def connect():
return sqlite3.connect('inventory.db')
# Function to create the 'items' table if it doesn't exist and populate it with initial data
def create_table():
conn = connect()
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS items (
item_no INTEGER PRIMARY KEY AUTOINCREMENT,
product_name TEXT NOT NULL,
category TEXT NOT NULL,
quantity INTEGER NOT NULL,
price REAL NOT NULL
)''')
# Check if the table is empty, if so, insert initial data
c.execute("SELECT count(*) FROM items")
row_count = c.fetchone()[0]
if row_count == 0:
initial_data = [
("2 COOL 4 SKOOL", "BTS", 10, 1200),
("DARK & WILD", "BTS", 10, 1100),
("LOVE YOURSELF: HER", "BTS", 10, 1400),
("SQUARE UP", "BLACKPINK", 10, 1500),
("THE ALBUM", "BLACKPINK", 10, 1300),
("KILL THIS LOVE", "BLACKPINK", 10, 1700),
("XOXO", "EXO", 10, 1200),
("GROWL", "EXO", 10, 1000),
("OBSESSION", "EXO", 10, 1600),
("BE US", "BGYO", 10, 1400),
("THE LIGHT", "BGYO", 10, 1100),
("FEEL GOOD", "BINI", 10, 1200),
("BORN TO WIN", "BINI", 10, 1000),
("FRIEND-SHIP", "HORI7ON", 10, 1600),
("PERFECT WORLD", "TWICE", 10, 1500),
("FEEL SPECIAL", "TWICE", 10, 1300),
("SIGNAL", "TWICE", 10, 1900),
("RINGO", "ITZY", 10, 1400),
("NOT SHY", "ITZY", 10, 1200),
("CHESHIRE", "ITZY", 10, 1000)
]
c.executemany('''INSERT INTO items (product_name, category, quantity, price)
VALUES (?, ?, ?, ?)''', initial_data)
conn.commit()
conn.close()
# Function to search for items based on search criteria
def search_items(search_text, item_no, category):
conn = connect()
c = conn.cursor()
# Search for items matching the search criteria
c.execute("SELECT * FROM items WHERE LOWER(product_name) LIKE ? AND item_no LIKE ? AND (category = ? OR ? = 'All')",
('%' + search_text.lower() + '%', '%' + item_no + '%', category, category))
filtered_items = c.fetchall()
conn.close()
return filtered_items
# Function to retrieve all records from the 'items' table
def show_all_records():
conn = connect()
c = conn.cursor()
# Retrieve all records from the 'items' table
c.execute("SELECT * FROM items")
all_items = c.fetchall()
conn.close()
return all_items
# Function to add a new item to the 'items' table
def add_item(item_data):
conn = connect()
c = conn.cursor()
# Determine the new item number by finding the maximum item number and adding 1
last_item_no = c.execute("SELECT MAX(item_no) FROM items").fetchone()[0]
if last_item_no is None:
new_item_no = 1
else:
new_item_no = last_item_no + 1
# Insert the new item into the 'items' table
c.execute('''INSERT INTO items (item_no, product_name, category, quantity, price)
VALUES (?, ?, ?, ?, ?)''', (new_item_no,) + item_data)
conn.commit()
conn.close()
# Function to remove a product from the 'items' table
def remove_product(item_no):
conn = connect()
c = conn.cursor()
# Delete the product with the specified item number from the 'items' table
c.execute("DELETE FROM items WHERE item_no=?", (item_no,))
conn.commit()
conn.close()
# Function to update a product in the 'items' table
def update_item(item_no, new_item_data):
conn = connect()
c = conn.cursor()
# Update the product with the specified item number in the 'items' table
c.execute("UPDATE items SET product_name=?, category=?, quantity=?, price=? WHERE item_no=?",
(new_item_data[1], new_item_data[2], new_item_data[3], new_item_data[4], item_no))
conn.commit()
conn.close()