This repository was archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbConnection.py
More file actions
52 lines (44 loc) · 1.58 KB
/
DbConnection.py
File metadata and controls
52 lines (44 loc) · 1.58 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
50
51
import sqlite3, re
class DbConnection:
def __init__(self, dbFile, commitOnClose = True):
try:
self.commitOnClose = commitOnClose
self.fileName = dbFile
self.connection = sqlite3.connect(self.fileName)
self.connection.create_function("REGEXP", 2, self.sqliteRegexp)
self.connection.row_factory = self.sqliteRowFactory
self.cursor = self.connection.cursor()
except sqlite3.Error as e:
print ('DB Error', e)
return False
except Exception as e:
print ('General Error', e)
return False
#finally:
# if self.connection:
# self.connection.close()
def __enter__(self):
return self
def __exit__(self, exitType, exitValue, exitTraceback):
self.close(self.commitOnClose)
def close(self, commit = True):
if self.connection:
if (commit):
self.connection.commit()
self.connection.close()
self.connection = None
def sqliteRegexp(self, pattern, subject):
rx = re.compile(pattern)
match = rx.search(subject)
#print("Pattern:", pattern, "Subject:", subject, "Match:", match)
if match:
#print("RE:", match.group(0))
#return match.group(0)
return True
else:
return None
def sqliteRowFactory(self, cursor, row):
dictionary = {}
for index, field in enumerate(cursor.description):
dictionary[field[0]] = row[index]
return dictionary