py.lib

Yohn Y. 2022-07-17 Parent:26a5160d6b6b Child:84b54a8a6d4c

29:b8fc22566efd Go to Latest

py.lib/db/sqlite.py

. Устанавливаем значение по умолчанию для нового лога

History
1 # coding: utf-8
2 import sqlite3
3 from sqlite3 import Error, IntegrityError
5 class DB:
6 def __init__(self, db_file):
7 self._conn = sqlite3.connect(db_file)
8 self._ex = self._conn.execute
9 self.commit = self._conn.commit
10 self.rollback = self._conn.rollback
12 # DB PREP
13 self._ex("PRAGMA journal=WAL")
14 self._ex("PRAGMA foreign_keys=ON")
15 self.commit()
17 def __del__(self):
18 try:
19 self.rollback()
20 self._conn.close()
21 except:
22 pass
24 def __call__(self, *a, **kwa):
25 cur = self._conn.cursor()
26 cur.execute(*a, **kwa)
27 return cur
29 def cq(self, *a, **wa):
30 try:
31 res = self(*a, **wa)
32 self.commit()
33 return res
34 except Error as e:
35 self.rollback()
36 raise e