-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
60 lines (49 loc) · 1.4 KB
/
database.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
from mysql import connector
def connect():
return connector.connect(
host= '',
user= '',
passwd= '',
database= ''
)
def insert_product(product):
db = connect()
mycursor = db.cursor()
try:
mycursor.execute("INSERT INTO Products (name, price) VALUES (%s,%f)",(product.name, product.price))
db.commit()
except Exception as error:
print("Error: ",end='')
print(error)
finally:
mycursor.close()
db.close()
def insert_departament(departament):
db = connect()
mycursor = db.cursor()
try:
mycursor.execute("INSERT INTO Departaments (name) VALUES (%s)",(departament.name))
db.commit()
except Exception as error:
print("Error: {}.".format(error))
finally:
mycursor.close()
db.close()
def select_product():
db = connect()
mycursor = db.cursor()
try:
mycursor.execute("SELECT name FROM Products")
products = mycursor.fetchall()
except Exception as error:
print("Error: {}.".format(error))
return products
def select_departament():
db = connect()
mycursor = db.cursor()
try:
mycursor.execute("SELECT name FROM Departaments")
departaments = mycursor.fetchall()
except Exception as error:
print("Error: {}.".format(error))
return departaments