-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQLConnector.py
134 lines (106 loc) · 3.15 KB
/
MySQLConnector.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
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 17 19:39:11 2020
@author: Jorge Esteban Mendoza Ortiz
"""
from mysql.connector.connection import MySQLConnection
from mysql.connector import errorcode
import mysql.connector
import traceback
import logging
config = {
'user': 'root',
'password': 'password',
'host': 'localhost',
'database': 'taxi'
}
class MySQLConnector:
"""
Convenience wrapper that creates a connection with the
database taxi at localhost and is capable of performing
operations to the MySQL database.
"""
def __init__(self):
try:
self.connection = MySQLConnection(**config)
print("Connection created")
self.cursor = self.connection.cursor(dictionary=True)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
def query(self, query, args=None):
"""
Executes queries given a string with the query
Input
query: str with MySQL query
args=None: arguments to pass to cursor
Output
None
"""
if args is None:
self.cursor.execute(query)
else:
self.cursor.execute(query, args)
# def fetchone(self):
# return self.cursor.fetchone()
#
# def fetchall(self):
# return self.cursor.fetchall()
def insert(self, data):
"""
Inserts a dictionary into the trips table
Input
data: dict with proper registry
Output
None
"""
fields, values = self.str_helper(data)
query = ("INSERT INTO trips "
"(" + fields + ") "
"VALUES (" + values + ")")
try:
self.query(query)
self.connection.commit()
except Exception as e:
logging.error(traceback.format_exc())
self.query(query)
# def update(self, query, args):
# self.query(query, args)
# self.connection.commit()
#
# def delete(self, query):
# self.query(query)
# self.connection.commit()
#
def close(self):
self.cursor.close()
self.connection.close()
print("Connection closed")
@staticmethod
def str_helper(data):
fields = str()
values = str()
for field in data.keys():
fields += f"{field}, "
for value in data.values():
if type(value) is int or type(value) is float:
values += f"{value}, "
else:
values += f"\'{value}\', "
fields = fields.rstrip(", ")
values = values.rstrip(", ")
return fields, values
if __name__ == '__main__':
# Example
cnx = MySQLConnector()
cnx.query("""
select * from trips
limit 3
""")
for row in cnx.cursor:
print(row)
cnx.close()