py.lib

Yohn Y. 2020-07-24 Child:50ff84d0bc56

13:cab7fedf8432 Go to Latest

py.lib/db/sqlite.py

.. Реструктуризация

History
1 # coding: utf-8
2 import sqlite3
4 class DB:
5 def __init__(self, db_file):
6 self._con = sqlite3.connect(db_file)
7 self._ex = self._con.execute
8 self.commit = self._con.commit
9 self.rollback = self._con.rollback
11 # DB PREP
12 self._ex("PRAGMA journal=WAL")
13 self.commit()
15 def __del__(self):
16 try:
17 self.rollback()
18 self._con.close()
19 except:
20 pass
22 def __call__(self, *a, **kwa):
23 cur = self._con.cursor()
24 cur.execute(*a, **kwa)
25 return cur
27 def cq(self, *a, **wa):
28 try:
29 res = self(*a, **wa)
30 self.commit()
31 return res
32 except Exception as e:
33 self.rollback()
34 raise e