-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
57 lines (49 loc) · 1.44 KB
/
db.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
import sqlite3
import mysql.connector
class SQLHandler(object):
def __init__(self,
file_name="gcir_news_db",
host="192.168.0.10",
port="3306",
user="gcir_api_admin",
password="gcirsaveillusts"):
self.db = mysql.connector.connect(
host=host,
port=port,
user=user,
password=password,
database=file_name
)
self.conn = self.db.cursor()
def get(self, sql, data=None):
if data == None:
self.conn.execute(sql)
else:
self.conn.execute(sql, data)
return self.conn.fetchall()
def commit(self):
self.db.commit()
def rollback(self):
self.db.rollback()
def edit(self, sql, data=None, autoCommit=True):
try:
if not data:
self.conn.execute(sql)
else:
self.conn.execute(sql, data)
if autoCommit:
self.db.commit()
return True
except Exception as e:
print(e)
self.db.rollback()
return False
def has(self, tableName, condition, data=None):
d = self.get("SELECT 'T' FROM %s WHERE %s" %
(tableName, condition), data)
if len(d) > 0:
return True
else:
return False
def close(self):
self.conn.close()