This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
database.py
146 lines (138 loc) · 4.9 KB
/
database.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- encoding: utf-8 -*-
'''
@Description: :数据库处理脚本
@Date :2020/10/08 20:22:08
@Author :a76yyyy
@version :1.0
'''
import pymysql
import sqlite3
import os
from configs import mysql
from func_timeout import func_set_timeout,exceptions
import sys
class mysql_Database:
host = mysql.host
port = mysql.port
user = mysql.user
password = mysql.password
charset = mysql.charset
def __init__(self,*args):
if len(args) == 1:
self.db = args[0]
self.connection = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.password, database=self.db, charset=self.charset)
elif len(args) == 2:
self.db = args[0]
self.connect_timeout = args[1]
self.connection = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.password, database=self.db, charset=self.charset, connect_timeout=self.connect_timeout)
elif len(args) == 3:
self.db = args[0]
self.connect_timeout = args[1]
self.read_timeout = args[2]
self.connection = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.password, database=self.db, charset=self.charset, connect_timeout=self.connect_timeout, read_timeout=self.read_timeout)
else:
print('参数输入错误')
sys.exit()
self.cursor = self.connection.cursor()
def insert(self, query, params):
try:
self.cursor.executemany(query, params)
self.connection.commit()
except Exception as e:
print(e)
self.connection.rollback()
def execute(self,code):
try:
self.cursor.execute(code)
self.connection.commit()
except Exception as e:
print(e)
self.connection.rollback()
def executemany(self,code,slist):
try:
self.cursor.executemany(code,slist)
self.connection.commit()
except Exception as e:
print(e)
self.connection.rollback()
def query(self, query, *args):
self.cursor = self.connection.cursor(pymysql.cursors.DictCursor)# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
result = None
timeout = None
if args and len(args) == 1:
timeout = args[0]
if timeout:
@func_set_timeout(timeout)
def timelimited():
self.cursor.execute(query)
result = self.cursor.fetchall()
return result
try:
result = timelimited()
except exceptions.FunctionTimedOut:
print("timeout!")
self.cursor.close()
return result
else:
self.cursor.execute(query)
result = self.cursor.fetchall()
return result
def __del__(self):
self.connection.close()
class sqlite3_Database:
def __init__(self,db_file,connect_timeout=5.0):
self.connection = sqlite3.connect(db_file,timeout=connect_timeout)
self.cursor = self.connection.cursor()
def insert(self, query, params):
try:
self.cursor.executemany(query, params)
self.connection.commit()
except Exception as e:
print(e)
self.connection.rollback()
def execute(self,code):
try:
self.cursor.execute(code)
self.connection.commit()
except Exception as e:
print(e)
self.connection.rollback()
def executemany(self,code,slist):
try:
self.cursor.executemany(code,slist)
self.connection.commit()
except Exception as e:
print(e)
self.connection.rollback()
@staticmethod
def dictFactory(cursor,row):
"""将sql查询结果整理成字典形式"""
d={}
for index,col in enumerate(cursor.description):
d[col[0]]=row[index]
return d
def query(self, query, *args):
self.connection.row_factory=self.dictFactory # 得到一个可以执行SQL语句并且将结果作为字典返回的游标
self.cursor = self.connection.cursor()
result = None
timeout = None
if args and len(args) == 1:
timeout = args[0]
if timeout:
@func_set_timeout(timeout)
def timelimited():
self.cursor.execute(query)
result = self.cursor.fetchall()
return result
try:
result = timelimited()
except exceptions.FunctionTimedOut:
print("timeout!")
self.cursor.close()
return result
else:
self.cursor.execute(query)
result = self.cursor.fetchall()
return result
def __del__(self):
self.connection.close()