-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBcm.py
More file actions
45 lines (33 loc) · 1.15 KB
/
Copy pathDBcm.py
File metadata and controls
45 lines (33 loc) · 1.15 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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sqlite3
# db_config = {
# 'dbname':DB_FILE,
# 'tbname':LOG_TABLE_NAME
# }
class DataAccessException(Exception):
pass
class UseDatabase(object):
"""docstring for UseDatabase"""
def __init__(self, config: dict)->None:
super(UseDatabase, self).__init__()
self.config = config
def __enter__(self) -> 'cursor':
try:
self.conn = sqlite3.connect(self.config['dbname'])
self.cursor = self.conn.cursor()
return self.cursor
except Exception as e:
raise DataAccessException(e)
def __exit__(self, exc_type, exc_value, exc_trace) -> None:
self.conn.commit()
self.cursor.close()
self.conn.close()
if exc_type is sqlite3.OperationalError:
print('some thing error in with')
raise DataAccessException('error in exit')
elif exc_type:
print('unhandled exceptions', str(type(exc_value)))
raise exc_type(exc_value)
if __name__ == '__main__':
pass