-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBHandler_for_sqlite.py
44 lines (34 loc) · 1.07 KB
/
DBHandler_for_sqlite.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
import sqlite3
db_file_path = 'demo.db'
class DBManager(object):
def __init__(self, **params):
self.db = params['db_file_path']
self.conn = None
self.cursor = None
def __new__(cls, *args, **kwargs):
if not hasattr(DBManager, "_instance"):
DBManager._instance = object.__new__(cls)
return DBManager._instance
def connect(self):
self.conn = sqlite3.connect(self.db)
self.cursor = self.conn.cursor()
return self.conn, self.cursor
def execute(self, sql):
print sql
try:
conn, cursor = self.connect()
cursor.execute(sql)
conn.commit()
return cursor.fetchall()
except Exception as e:
print e
self.conn.rollback()
exit(1)
finally:
self.close_db()
def close_db(self):
self.conn.close()
if __name__ == '__main__':
s1 = DBManager(db_file_path=db_file_path)
result = s1.execute("select * from parsed_logs where msg like '% sfp %';")
print len(result)