-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlpy.py
54 lines (44 loc) · 1.38 KB
/
mysqlpy.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
from pymysql import *
class mysqlhelp:
def __init__(self, database, host='localhost', user='root', password='123456', charset='utf8', port=3306):
self.database = database
self.host = host
self.user = user
self.password = password
self.charset = charset
self.port = port
# 链接数据库创建
def open(self):
self.conn = connect(host=self.host, user=self.user, password=self.password,
database=self.database, charset=self.charset, port=self.port)
self.cur = self.conn.cursor()
# 关闭方法
def close(self):
self.cur.close()
self.conn.close()
# 执行SQL语句
def work(self, sql, L=[]):
self.open()
try:
self.cur.execute(sql, L)
self.conn.commit()
# print("ok")
except Exception as e:
self.conn.rollback()
print("Failed", e)
self.close()
# 执行查询方法
def getall(self, sql, L=[]):
self.open()
self.cur.execute(sql, L)
# print("ok")
result = self.cur.fetchall()
self.close()
return result
# if __name__ == "__main__":
# 测试
# mysql = mysqlhelp("MOSHOU")
# insert = 'insert into hero(id) values(89);'
# mysql.work(insert)
# select = 'select name from hero'
# print(mysql.getall(select))