-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathproducts_dao.py
More file actions
57 lines (50 loc) · 1.53 KB
/
products_dao.py
File metadata and controls
57 lines (50 loc) · 1.53 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
from sql_connection import get_sql_connection
def get_all_products(connection):
cursor = connection.cursor()
query = """
SELECT
p.product_id,
p.name AS product_name,
p.uom_id,
p.price_per_unit,
u.uom_name
FROM
products AS p
INNER JOIN
uom AS u ON p.uom_id = u.uom_id
"""
cursor.execute(query)
response = []
for (product_id, name, uom_id, price_per_unit, uom_name) in cursor:
response.append({
'product_id': product_id,
'name': name,
'uom_id': uom_id,
'price_per_unit': price_per_unit,
'uom_name': uom_name
})
cursor.close()
return response
def insert_new_product(connection, product):
cursor = connection.cursor()
query = ("INSERT INTO products "
"(name, uom_id, price_per_unit)"
"VALUES (%s, %s, %s)")
data = (product['product_name'], product['uom_id'], product['price_per_unit'])
cursor.execute(query, data)
connection.commit()
return cursor.lastrowid
def delete_product(connection, product_id):
cursor = connection.cursor()
query = ("DELETE FROM products where product_id=" + str(product_id))
cursor.execute(query)
connection.commit()
return cursor.lastrowid
if __name__ == '__main__':
connection = get_sql_connection()
# print(get_all_products(connection))
print(insert_new_product(connection, {
'product_name': 'potatoes',
'uom_id': '1',
'price_per_unit': 10
}))