-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBFunctions.py
60 lines (42 loc) · 2.44 KB
/
DBFunctions.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "MPZinke"
########################################################################################################################
# #
# created by: MPZinke #
# on .. #
# #
# DESCRIPTION: #
# BUGS: #
# FUTURE: #
# #
########################################################################################################################
def __CLOSE__(cnx, cursor):
cursor.close();
cnx.close();
def __CONNECT__(user : str, password : str, db_name : str):
import mysql.connector;
cnx = mysql.connector.connect(user=user, password=password, host="localhost", port="3306", database=db_name);
return cnx, cnx.cursor(buffered=True);
def __UTILITY__associate_query(cursor):
headers = [header[0] for header in cursor._description];
return [{header : (row[x] if row[x] else None) for x, header in enumerate(headers)} for row in cursor._rows];
def __UTILITY__query(cursor : object, query : str, *params) -> list:
if(len(params)): cursor.execute(query, params);
else: cursor.execute(query);
return __UTILITY__associate_query(cursor);
def main():
cnx, cursor = __CONNECT__("root", "mysql", "SYAD");
query = "SELECT * FROM `Restaurant`;";
restaurants = __UTILITY__query(cursor, query);
print(restaurants, end="\n\n\n");
# get corners for restaurants
query = "SELECT * FROM `RestaurantPoints` WHERE `Restaurant.id` = %s;";
for restaurant in restaurants:
points = __UTILITY__query(cursor, query, restaurant["id"]);
print(type(points))
print(points[0]);
print(points);
__CLOSE__(cnx, cursor);
if __name__ == "__main__":
main()