-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmysql_util.py
39 lines (31 loc) · 1.25 KB
/
mysql_util.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
import pymysql
def execute(host, user, password, sql, port=3306, charset="utf8"):
try:
connection, cursor = execute_for_db(host, port, user, password, charset, sql)
cursor.execute(sql)
finally:
close(connection, cursor)
def fetchone(host, user, password, sql, port=3306, charset="utf8"):
try:
connection, cursor = execute_for_db(host, port, user, password, charset, sql)
cursor.execute(sql)
return cursor.fetchone()
finally:
close(connection, cursor)
def fetchall(host, user, password, sql, port=3306, charset="utf8"):
try:
connection, cursor = execute_for_db(host, port, user, password, charset, sql)
return cursor.fetchall()
finally:
close(connection, cursor)
def close(connection, cursor):
if(cursor != None):
cursor.close()
if(connection != None):
connection.close()
def execute_for_db(host, port, user, password, charset, sql):
connection = pymysql.connect(host=host, port=port, user=user, password=password, charset=charset, cursorclass=pymysql.cursors.DictCursor)
cursor = connection.cursor()
cursor.execute(sql)
return connection, cursor
print(fetchall("192.168.11.130", "yangcg", "yangcaogui", "show slave status;"))