-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_connect.py
170 lines (128 loc) · 4.31 KB
/
mysql_connect.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from mysql.connector import MySQLConnection, Error
from collections import OrderedDict
from config import read_db_config
class MysqlPython(object):
"""
Python Class for connecting with MySQL server and accelerate development project using MySQL
Extremely easy to learn and use, friendly construction.
"""
__instance = None
__host = None
__user = None
__password = None
__database = None
__cursor = None
__connection = None
def __new__(cls, *args, **kwargs):
if not cls.__instance or not cls.__database:
cls.__instance = super(MysqlPython, cls).__new__(cls, *args, **kwargs)
return cls.__instance
# End def __new__
# def __init__():
def __open(self):
try:
db_config = read_db_config()
cnx = MySQLConnection(**db_config)
self.__connection = cnx
self.__cursor = cnx.cursor()
except Error as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
# End def __open
def __close(self):
self.__cursor.close()
self.__connection.close()
# End def __close
def get_connection(self):
try:
db_config = read_db_config()
self.__connection = MySQLConnection(**db_config)
except Error as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
return self.__connection
def select(self, table, where=None, *args, **kwargs):
query = 'SELECT '
keys = args
values = tuple(kwargs.values())
l = len(keys) - 1
for i, key in enumerate(keys):
query += "`" + key + "`"
if i < l:
query += ","
# End for keys
query += ' FROM %s' % table
if where:
query += " WHERE %s" % where
# End if where
self.__open()
self.__cursor.execute(query, values)
items = self.__cursor.fetchall()
self.__close()
return items
# End def select
def update(self, table, where=None, *args, **kwargs):
query = "UPDATE %s SET " % table
keys = kwargs.keys()
values = tuple(kwargs.values()) + tuple(args)
l = len(keys) - 1
for i, key in enumerate(keys):
query += "`" + key + "` = %s"
if i < l:
query += ","
# End if i less than 1
# End for keys
query += " WHERE %s" % where
self.__open()
self.__cursor.execute(query, values)
self.__connection.commit()
# Obtain rows affected
update_rows = self.__cursor.rowcount
self.__close()
return update_rows
# End function update
def insert(self, table, *args, **kwargs):
values = None
query = "INSERT INTO %s " % table
if kwargs:
keys = kwargs.keys()
values = tuple(kwargs.values())
query += "(" + ",".join(["`%s`"] * len(keys)) % tuple(keys) + ") VALUES (" + ",".join(
["%s"] * len(values)) + ")"
elif args:
values = args
query += " VALUES(" + ",".join(["%s"] * len(values)) + ")"
self.__open()
self.__cursor.execute(query, values)
self.__connection.commit()
self.__close()
return self.__cursor.lastrowid
# End def insert
def delete(self, table, where=None, *args):
query = "DELETE FROM %s" % table
if where:
query += ' WHERE %s' % where
values = tuple(args)
self.__open()
self.__cursor.execute(query, values)
self.__connection.commit()
# Obtain rows affected
delete_rows = self.__cursor.rowcount
self.__close()
return delete_rows
# End def delete
def select_advanced(self, sql, *args):
od = OrderedDict(args)
query = sql
values = tuple(od.values())
self.__open()
self.__cursor.execute(query, values)
items = self.__cursor.fetchall()
number_rows = self.__cursor.rowcount
number_columns = len(self.__cursor.description)
if number_rows >= 1 and number_columns > 1:
result = [item for item in items]
else:
result = [item[0] for item in items]
self.__close()
return result
# End def select_advanced
# End class