-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMysql.py
58 lines (47 loc) · 1.46 KB
/
Mysql.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
#coding=utf-8
import pymysql
class Mysql:
database = {
'host': 'localhost',
'user': 'root',
'passwd': '123456',
'db': 'we',
'charset': 'utf8mb4',
'port': 3306
}
connector = None
cursor = None
def __init__(self):
self.connectMysql()
self.cursor = self.connector.cursor()
return
def connectMysql(self):
try:
self.connector = pymysql.connect(host=self.database['host'], user=self.database['user'], passwd=self.database['passwd'], db=self.database['db'], charset=self.database['charset'], port=self.database['port'])
except Exception:
print('链接数据库失败')
print(Exception)
def execute(self, sql):
try:
self.cursor.execute(sql)
self.connector.commit()
except Exception:
self.connector.rollback()
print('sql语句执行失败')
print(Exception)
print(sql)
def query(self, sql):
try:
result = self.cursor.execute(sql)
return result
except Exception:
print('sql语句执行失败')
print(Exception)
print(sql)
def getOne(self, sql):
sql = sql + ' limit 1'
self.query(sql)
return self.cursor.fetchone()
def closeConnect(self):
self.cursor.close()
self.connector.close()