-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
105 lines (92 loc) · 3.04 KB
/
database.py
File metadata and controls
105 lines (92 loc) · 3.04 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
import mysql.connector
from constants import *
from api_requests import *
offfile = FILE
usergf = MAIN_USER
hostgf = MAIN_HOST
passwrdgf = MAIN_PASSWORD
databasegf = MAIN_DATABASE
class Database:
"""A class that allow user to connect to the database or create a new one from the database file, with
two staticmethods, or query database in order to display required data."""
db_user = usergf
db_password = passwrdgf
db_host = hostgf
db_name = databasegf
db_file = offfile
@staticmethod
def loging():
"""Staticmethod for login"""
mydb = mysql.connector.connect(
host=Database.db_host,
user=Database.db_user,
password=Database.db_password,
database=Database.db_name,
)
@staticmethod
def create_database():
"""Staticmethod for create database"""
mydb = mysql.connector.connect(
host=Database.db_host, user=Database.db_user, password=Database.db_password
)
cursor = mydb.cursor()
sql = open(offfile).read()
cursor.execute(sql)
def __init__(self):
"""Setting Cursor's parameters into class constructor"""
MySQLConfig = {
"user": usergf,
"password": passwordGF,
"host": hostgf,
"database": databasegf,
}
self.mydb = mysql.connector.connect(**MySQLConfig)
self.cursor = self.mydb.cursor()
def display_categories(self):
"""Method used for displaying categories"""
self.cursor.execute(QUERY_DISPLAY_CATEGORY)
for id, name in self.cursor:
print(id, ".....", name)
def display_saved(self):
"""Method used for displaying substitute table"""
self.cursor.execute(QUERY_DISPLAY_SAVED)
for (
saved_product_id,
saved_product_name,
saved_product_grade,
saved_substitute_id,
saved_substitute_name,
saved_substitute_grade,
) in self.cursor:
print(
"Produit à substituer :",
"\n",
"ID :",
saved_product_id,
"...",
"Nom du produit :",
saved_product_name,
"...",
"Grade nutritionnel :",
saved_product_grade,
"\n",
"Produit de substitution :",
"\n",
"ID :",
saved_substitute_id,
"...",
"Nom du produit :",
saved_substitute_name,
"...",
"Grade nutritionnel :",
saved_substitute_grade,
"\n",
)
def select_category(self, cat_id):
"""Method used for displaying all products from a selected category"""
self.cursor.execute(QUERY_SELECT_CATEGORY, cat_id)
for id, name in self.cursor:
print(id, ".....", name)
def db_cursor_closed(self):
self.cursor.close()
self.mydb.close()