Repository navigation
Expand file tree
/
Copy pathMySQLDB.py
More file actions
56 lines (45 loc) · 1.66 KB
/
Copy pathMySQLDB.py
File metadata and controls
56 lines (45 loc) · 1.66 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
import pymysql
class MySQLDB:
def __new__(cls, host, user, password, database):
""" test connection """
try:
connect = pymysql.connect(host = host,
user = user,
password = password,
database = database)
except Exception as e:
print("Failed to connect database: ", e)
return None
else:
connect.close()
return super().__new__(cls)
def __init__(self, host, user, password, database):
""" initialize args """
self.host, self.user, self.password, self.database = host, user, password, database
def query(self,sqls):
""" execute the sqls """
assert isinstance(sqls,list), 'sqls must be a list'
connect = pymysql.connect(host = self.host,
user = self.user,
password = self.password,
database = self.database,
cursorclass = pymysql.cursors.DictCursor)
cursor = connect.cursor()
try:
for sql in sqls:
cursor.execute(sql)
data = cursor.fetchall()
except Exception as e:
print("Failed to query: ", e)
data = None
finally:
connect.close()
return data
# if __name__=="__main__":
# # A TEST FOR MySQLDB
# db_mysql = MySQLDB(host='localhost',
# user='root',
# password='password',
# database='mysql')
# sql="""select * from user"""
# print(db_mysql.query([sql]))