-
Notifications
You must be signed in to change notification settings - Fork 1
/
DBUtils.py
42 lines (33 loc) · 1.33 KB
/
DBUtils.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
#!/usr/bin/python3
# encoding='utf-8'
# author:weibk
# @time:2021/10/6 19:18
import pymysql
class DatabaseConnect:
def __init__(self, host="localhost", post=3306, user="root",
password="root",
database="", charset="utf8", cursorclass="Cursor"):
self.connect = pymysql.connect(host=host, port=post, user=user,
password=password, database=database,
charset=charset)
self.cursor = self.connect.cursor(cursor=cursorclass)
def __enter__(self):
return self.cursor
def __exit__(self, exc_type, exc_val, exc_tb):
self.connect.commit()
self.cursor.close()
self.connect.close()
def update(sql, params, database="db"):
with DatabaseConnect(password="123456", database=database,
cursorclass=pymysql.cursors.DictCursor) as cursor:
cursor.execute(sql, params)
def query(sql, parmas=[], size=0, database="db"):
with DatabaseConnect(password="123456", database=database,
cursorclass=pymysql.cursors.DictCursor) as cursor:
cursor.execute(sql, parmas)
if size == 0:
return cursor.fetchall()
elif size == 1:
return cursor.fetchone()
else:
return cursor.fetchmany(size)