-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amelia.py
205 lines (185 loc) · 8.32 KB
/
amelia.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
########################################################
# WELCOME TO AMELIA DATA ACCESS! #
# ver. 2.1.0.0 - Brigadeiro #
# #
# -WHAT DOES THIS LIB DO?- #
# MAKE YOUR LIFE EASIER WITH SIMPLE CONNECTION #
# WITH MARIADB #
# #
# -WHY THE NAME 'AMELIA'?- #
# SEE https://bit.ly/2WgIsm2 - NO VIRUS, TRUST ME #
# #
# -WHY CHOOSE AMELIA?- #
# BECAUSE IT'S SIMPLE, FREE AND OPEN... DUH? #
# #
# -HOW DO I USE IT?- #
# FIRST, IMPORT IT TO YOUR .py #
# SECOND, TWO WAYS: #
# _AMELIAGO #
# USING THIS "MOTHER METHOD", YOU JUST USE THE LIST #
# WITH CONNECTION AND WHAT YOU WANNA DO. #
# #
# _GO THROUGH ALL #
# WE RECOMMEND THIS FOR SOMETHING EDUCATION, DIDACTIC #
# WORK THE SAME AS THE AMELIAGO BUT WITH STEPS MADE #
# BY YOU, LIKE THIS ONE: #
# #
#conn = baseConnect('0.0.0.0', 'root', 'root', 'test') #
#lcursor = conn.cursor() #
#query = simpleSqlOpen(lcursor, "select * from test") #
#content = (query) #
# #
# ---- CHANGE LOG ---- #
# #
# - 2.1.0.0 - Brigadeiro - #
# - BUGFIX - I forgot to update simpleSqlOpen #
# #
# - 2.0.0.0 - Brigadeiro - #
# - BUGFIX - Add column names at the resultset #
# - NEW FUNCION - Multi RecordSet embedded now #
# #
# - 1.0.1.0 - Cocada - Amelia's birth #
# #
# Created by RealMr_Glasses #
########################################################
import mysql.connector as mariadb
#status functions -------------------------------------------------------------------------------------------------------------
def isAlive(Aconfig):
"Alive? Maybe. - Configuration like preloadConnect - list with HOST, USER, PASS and DB"
try:
mariadb_connection = mariadb.connect(host=Aconfig[0], user=Aconfig[1], password=Aconfig[2], database=Aconfig[3])
mariadb_connection.close()
return 'Yup.'
except BaseException as e:
return 'Nope. Error: ' + str(e)
def isAliveFV(Ahost, Auser, Apass, Adb):
"Alive? Maybe. (Full version)"
try:
mariadb_connection = mariadb.connect(host=Ahost, user=Auser, password=Apass, database=Adb)
mariadb_connection.close()
return 'Yup.'
except BaseException as e:
return 'Nope. Error: ' + str(e)
#Connection functions ---------------------------------------------------------------------------------------------------------
def baseConnect(Ahost, Auser, Apass, Adb):
"""Create a cursor for the conection with database. Don't forget to make a return.close() """
try:
mariadb_connection = mariadb.connect(host=Ahost, user=Auser, password=Apass, database=Adb)
#cursor = mariadb_connection.cursor()
return mariadb_connection
except BaseException as e:
return 'Error: ' + str(e)
#return None
def preloadConnect(Aconfig):
"""Create a cursor for the conection with database with a preload configuration. Configuration: list with HOST, USER, PASS and DB.Don't forget to make a return.close()"""
try:
mariadb_connection = mariadb.connect(host=Aconfig[0], user=Aconfig[1], password=Aconfig[2], database=Aconfig[3])
#cursor = mariadb_connection.cursor()
return mariadb_connection
except BaseException as e:
return 'Error: ' + str(e)
#return None
#True SQL functions
def simpleSqlOpen(Acursor, Atext):
"""Returns a SQL query with all lines."""
try:
Acursor.execute(Atext)
result = Acursor.fetchall()
listR = []
for result in Acursor.stored_results():
row_headers = [x for x in result.column_names] #column names
resultSet = result.fetchall()
dataSet = []
for line in resultSet:
dataSet.append(dict(zip(row_headers,line)))
listR.append(dataSet)
return listR #Multi RecordSet embedded
#return result
except BaseException as e:
return 'Error: ' + str(e)
def simpleSqlExec(Acursor, Atext):
"""Execute a SQL query. Don't forget the connection.commit() or connection.rollback() after this command."""
try:
Acursor.execute(Atext)
Acursor.fetchall()
# result.commit()
return True
except BaseException as e:
return 'Error: ' + str(e)
def simpleSqlProcOpen(Acursor, Aproc, Aparams):
"""Execute a procedure in and returns a SQL query with all lines."""
try:
Acursor.callproc(Aproc, Aparams)
listR = []
for result in Acursor.stored_results():
row_headers = [x for x in result.column_names] #column names
resultSet = result.fetchall()
dataSet = []
for line in resultSet:
dataSet.append(dict(zip(row_headers,line)))
listR.append(dataSet)
return listR #Multi RecordSet embedded
except BaseException as e:
return 'Error: ' + str(e)
def simpleSqlProcExec(Acursor, Aproc, Aparams):
"""Execute a SQL stored procedures. Don't forget the connection.commit() or connection.rollback() after this command."""
try:
Acursor.callproc(Aproc, Aparams)
Acursor.fetchall()
# result.commit()
return True
except BaseException as e:
return 'Error: ' + str(e)
#AMELIAGO - Complete connections to sql------------------------------------------------------------------------------------------------
def ameliaGoOpenSql(Aconfig, Atext):
"""Connect and return a simple select from MariaDB. Always check for errors, please."""
conn = preloadConnect(Aconfig)
if isinstance(conn, str):
return conn
else:
cur = conn.cursor()
generalResult = simpleSqlOpen(cur, Atext)
cur.close()
conn.close()
return generalResult
def ameliaGoExecSql(Aconfig, Atext):
"""Connect and return true or error message from a simple exec from MariaDB. Always check for errors, please."""
conn = preloadConnect(Aconfig)
if isinstance(conn, str):
return conn
else:
cur = conn.cursor()
generalResult = simpleSqlExec(cur, Atext)
if generalResult:
conn.commit()
else:
conn.rollback()
cur.close()
conn.close()
return generalResult
def ameliaGoProcOpen(Aconfig, Aproc, Aparams):
"""Connect and return the resultSet or error from a stored procedure from MariaDB. Always check for errors, please."""
conn = preloadConnect(Aconfig)
if isinstance(conn, str):
return conn
else:
cur = conn.cursor()
generalResult = simpleSqlProcOpen(cur, Aproc, Aparams)
cur.close()
conn.close()
return generalResult
def ameliaGoProcExec(Aconfig, Aproc, Aparams):
"""Connect and execute the stored procedure from MariaDB. Always check for errors, please."""
conn = preloadConnect(Aconfig)
if isinstance(conn, str):
return conn
else:
cur = conn.cursor()
generalResult = simpleSqlProcExec(cur, Aproc, Aparams)
if generalResult:
conn.commit()
else:
conn.rollback()
cur.close()
conn.close()
return generalResult