-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect.py
More file actions
49 lines (43 loc) · 1.33 KB
/
connect.py
File metadata and controls
49 lines (43 loc) · 1.33 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
import pymysql
class ConnectMysql(object):
def __init__(self,host,port,db,user,password):
self.host=host
self.port=port
self.db=db
self.user=user
self.password=password
def __enter__(self):
# 在进入的时候自动获取连接和cursor
conn = pymysql.connect(host=self.host, port=self.port, db=self.db, user=self.user, password=self.password)
cursor = conn.cursor()#pymysql.cursors.DictCursor)
# conn.autocommit = False
self.db = conn
self.cursor = cursor
return self
def __exit__(self, *exc_info):
self.cursor.close()
self.db.close()
def mult_query(s,
host='localhost',
port=3306,
db='supermr_db',
user='admin',
password='123456'):
s=''.join(s).split(';')[:-1]
with ConnectMysql(host,port,db,user,password) as m:
for a in s:
m.cursor.execute(a+';')
m.db.commit()
data = m.cursor.fetchall()
return data
def one_query(s,
host='localhost',
port=3306,
db='supermr_db',
user='admin',
password='123456'):
with ConnectMysql(host,port,db,user,password) as m:
m.cursor.execute(s)
m.db.commit()
data = m.cursor.fetchall()
return data