-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL_Interface.py
More file actions
174 lines (162 loc) · 6.43 KB
/
Copy pathMySQL_Interface.py
File metadata and controls
174 lines (162 loc) · 6.43 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
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
'''
Module : MySQL_Interface
Author : Ruthrapathy
Objective : To connect to MySQL database and execute a SQL
Functions : 1) Execute select statemnt
2) Execute Insert/Update/Delete statement
'''
import mysql.connector as sql
HOST = 'localhost'
USER = 'root'
PASSWORD = 'sankaram'
DATABASE = "inventory"
PORT = 3306 # default is 3306. Port must be specified if it is not 3306
def Execute_Select(select_sql):
'''
Function : Execute_Select
Author : Ruthrapathy
Objective : To execute a select statment after connecting to MySQL
Inputs : Properly formed select SQL statement
Returns : a 3 variables containing
a) first element as status. True means success, False means fail
b) second element as error message. Will have value only if status True is returned
c) third element as list of data returned by select statement
Examples : Here are some example on how to call the function
import MySQL_Interface
status, error_msg, data = MySQL_Interface.Execute_Select("select * from city")
'''
con = None
cur = None
status = True
error_msg = ""
data = []
try:
# 1) create a connector object by giving server/host, user-id, pwd and database
msg = "Error trying to connect to database: "
con = sql.connect (host=HOST, user=USER, passwd=PASSWORD,
database=DATABASE, port=PORT)
# 2) create a cursor
msg = "Error creating cursor: "
cur = con.cursor()
# 3) Execute
msg = "Error executing select statement: " + select_sql
cur.execute(select_sql)
# 4) Fetch data
msg = "Error fetching data: "
data = cur.fetchall()
status = True
except sql.Error as err:
error_msg = msg + err.msg
status = False
finally:
# 5) Now close open cur & con
if cur is not None :
cur.close()
if con is not None:
con.close()
data_lst = Convert_Tuple_To_List (data)
return status, error_msg, data_lst
def Execute_IUD(iud_sql):
'''
Function : Execute_IUD
Author : Ruthrapathy
Objective : To execute a insert/update/delete statment after connecting to MySQL in a Transaction
Inputs : Properly formed insert/update/delete SQL string statement in a list
iud_sql is a list containing 1 or more Insert/Update/Delete statements
Returns : a 2 values containing
a) first element as status. True means success, False means fail
b) second element as error message. Will have value only if status False is returned
Examples : Here are some example on how to call the function
import MySQL_Interface
status, error_msg = MySQL_Interface.Execute_Select("insert into city (id, name, countrycode, district, population) values ({}, '{}', '{}', '{}', {})".format(5001, 'Afghanistan', 'AFG', 'test2', 7002))
'''
con = None
cur = None
status = True
error_msg = ""
try:
# 1) create a connector object by giving server/host, user-id, pwd and database
msg = "Error trying to connect to database: "
con = sql.connect (host=HOST, user=USER, passwd=PASSWORD,
database=DATABASE, port=PORT)
# 2) create a cursor
msg = "Error creating cursor: "
cur = con.cursor()
# start transaction
con.start_transaction()
# 3) Execute
for sqls in iud_sql:
msg = "Error executing select statement: " + sqls
cur.execute(sqls)
# 4) if it comes here, ==> status ==> commit data
msg = "Error while committing data: "
con.commit()
status = True
except sql.Error as err:
error_msg = msg + err.msg
status = False
if con is not None:
con.rollback()
finally:
# 5) Now close open cur & con
if cur is not None :
cur.close()
if con is not None:
con.close()
return status, error_msg
def Execute_IUD(iud_sql):
'''
Function : Execute_IUD
Author : Ruthrapathy
Objective : To execute a insert/update/delete statment after connecting to MySQL in a Transaction
Inputs : Properly formed insert/update/delete SQL string statement in a list
iud_sql is a list containing 1 or more Insert/Update/Delete statements
Returns : a 2 values containing
a) first element as status. True means success, False means fail
b) second element as error message. Will have value only if status False is returned
Examples : Here are some example on how to call the function
import MySQL_Interface
status, error_msg = MySQL_Interface.Execute_Select("insert into city (id, name, countrycode, district, population) values ({}, '{}', '{}', '{}', {})".format(5001, 'Afghanistan', 'AFG', 'test2', 7002))
'''
con = None
cur = None
status = True
error_msg = ""
try:
# 1) create a connector object by giving server/host, user-id, pwd and database
msg = "Error trying to connect to database: "
con = sql.connect (host=HOST, user=USER, passwd=PASSWORD,
database=DATABASE, port=PORT)
# 2) create a cursor
msg = "Error creating cursor: "
cur = con.cursor()
# start transaction
con.start_transaction()
# 3) Execute
for sqls in iud_sql:
msg = "Error executing select statement: " + sqls
cur.execute(sqls)
# 4) if it comes here, ==> status ==> commit data
msg = "Error while committing data: "
con.commit()
status = True
except sql.Error as err:
error_msg = msg + err.msg
status = False
if con is not None:
con.rollback()
finally:
# 5) Now close open cur & con
if cur is not None :
cur.close()
if con is not None:
con.close()
return status, error_msg
def Convert_Tuple_To_List(data):
lst = []
for item in data:
l = []
for c in item:
l.append(c)
lst.append(l)
return lst