-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.1_hw8.py
156 lines (133 loc) · 4.18 KB
/
23.1_hw8.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import sqlite3
from sqlite3 import Error
def create_connection(db_name):
conn = None
try:
conn = sqlite3.connect(db_name)
except Error as e:
print(e)
return conn
def create_table(conn, sql):
try:
cursor = conn.cursor()
conn.execute(sql)
except Error as e:
print(e)
def create_products(conn, product):
try:
sql = '''
INSERT INTO products(product_name, price, quantity)
VALUES (?, ?, ?)
'''
cursor = conn.cursor()
conn.execute(sql, product)
conn.commit()
except Error as e:
print(e)
def change_quantity(conn,product):
try:
sql = '''
UPDATE products SET quantity = ? WHERE id = ?
'''
cursor = conn.cursor()
conn.execute(sql,product)
conn.commit()
except Error as e:
print(e)
def change_price(conn, product):
try:
sql = '''
UPDATE products SET price = ? WHERE id = ?
'''
cursor = conn.cursor()
conn.execute(sql,product)
conn.commit()
except Error as e:
print(e)
def delete_product(conn, product):
try:
sql = '''
DELETE FROM products WHERE id = ?
'''
cursor = conn.cursor()
conn.execute(sql, product)
conn.commit()
except Error as e:
print(e)
def select_all_products(conn):
try:
cursor = conn.cursor()
cursor.execute('''SELECT * FROM products''')
rows = cursor.fetchall()
for row in rows:
print(row)
except Error as e:
print(e)
def select_products(conn, product):
try:
sql = ('''SELECT * FROM products WHERE price < ? and quantity > ?''')
cursor = conn.cursor()
cursor.execute(sql, product)
rows = cursor.fetchall()
for row in rows:
print(row)
except Error as e:
print(e)
def search_product_by_product_name(conn, product_name):
try:
sql = ('''SELECT product_name FROM products WHERE product_name LIKE ?''')
cursor = conn.cursor()
cursor.execute(sql, ("%"+product_name+"%",))
rows = cursor.fetchall()
name = []
for row in rows:
name.append(row)
print(name)
except Error as e:
print(e)
def add_products(conn):
create_products(connection, ('chocolate', 100, 20))
create_products(connection, ('chocolate milk', 300, 15))
create_products(connection, ('chocolate cake', 400, 5))
create_products(connection, ('chocolate icecream', 50, 25))
create_products(connection, ('chocolate cream ', 250, 6))
create_products(connection, ('milk ', 50, 10))
create_products(connection, ('milk icecream ', 100, 5))
create_products(connection, ('curd ', 200,))
create_products(connection, ('curd with chocolate ', 350, 10))
create_products(connection, ('curd cake ', 400, 5))
create_products(connection, ('curd pancake ', 400, 5))
create_products(connection, ('curd cheese ', 400, 5))
create_products(connection, ('cheese ', 400, 5))
create_products(connection, ('cheese pizza', 400, 5))
create_products(connection, ('cheese hamburger ', 400, 5))
create_products(connection, ('cheese cake ', 400, 5))
sql_create_product_table = '''
CREATE TABLE products(
id INTEGER PRIMARY KEY AUTOINCREMENT,
product_name VARCHAR(200) NOT NULL,
price DOUBLE (10, 2) NOT NULL DEFAULT 0.0,
quantity INTEGER(5) NOT NULL DEFAULT 0
)
'''
db_name = 'hw.db'
connection = create_connection(db_name)
if connection is not None:
create_table(connection, sql_create_product_table)
add_products(connection)
select_all_products(connection)
print('all products')
select_products(connection, (100, 5))
print('products price < 100 quantity > 5')
search_product_by_product_name(connection, 'cake')
print('searched product by name')
change_quantity(connection, (67, 12))
print('changed quantity by id')
change_price(connection,(390, 4))
print('changed price by id')
delete_product(connection,(8,))
print('deleted product')
print('products after changes')
select_all_products(connection)
connection.close()
print('Successfully connected')