mirror of
https://github.com/norohind/jubilant-system.git
synced 2025-06-05 17:53:00 +03:00
solve problem with web and sqlite3.DatabaseError: database disk image is malformed
This commit is contained in:
parent
923b5d35e9
commit
d2076894d4
@ -3,6 +3,7 @@ import sqlite3
|
||||
import utils
|
||||
from . import sqlite_sql_requests
|
||||
import json
|
||||
import os
|
||||
from typing import Union
|
||||
from datetime import datetime
|
||||
|
||||
@ -10,23 +11,22 @@ from datetime import datetime
|
||||
class SqliteModel:
|
||||
db: sqlite3.Connection
|
||||
|
||||
def open_model(self) -> None:
|
||||
@property
|
||||
def db(self):
|
||||
"""
|
||||
This method must be called before any action on model
|
||||
One connection per request is only one method to avoid sqlite3.DatabaseError: database disk image is malformed.
|
||||
Connections in sqlite are extremely cheap (0.22151980000001004 secs for 1000 just connections and
|
||||
0.24141229999999325 secs for 1000 connections for this getter, thanks timeit)
|
||||
and don't require to be closed, especially in RO mode. So, why not?
|
||||
|
||||
:return:
|
||||
"""
|
||||
|
||||
self.db = sqlite3.connect('squads.sqlite', check_same_thread=False)
|
||||
self.db.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r))
|
||||
self.db.create_function('null_fdev', 1, self.null_fdev, deterministic=True)
|
||||
db = sqlite3.connect(f'file:{os.environ["SQLITE_DB"]}?mode=ro', check_same_thread=False, uri=True)
|
||||
db.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r))
|
||||
db.create_function('null_fdev', 1, self.null_fdev, deterministic=True)
|
||||
|
||||
def close_model(self) -> None:
|
||||
"""
|
||||
This method should be called before program exit
|
||||
:return:
|
||||
"""
|
||||
|
||||
self.db.close()
|
||||
return db
|
||||
|
||||
@staticmethod
|
||||
def null_fdev(value):
|
||||
|
@ -9,8 +9,6 @@ from EDMCLogging import get_main_logger
|
||||
logger = get_main_logger()
|
||||
logger.propagate = False
|
||||
|
||||
model.open_model()
|
||||
|
||||
|
||||
class SquadsInfoByTagHtml:
|
||||
def on_get(self, req: falcon.request.Request, resp: falcon.response.Response, tag: str, details_type: str) -> None:
|
||||
@ -72,13 +70,11 @@ class AppFixedLogging(falcon.App):
|
||||
|
||||
|
||||
application = AppFixedLogging()
|
||||
# application = falcon.App()
|
||||
application.add_route('/squads/now/by-tag/{details_type}/{tag}', SquadsInfoByTagHtml())
|
||||
|
||||
application.add_route('/api/squads/now/by-tag/{details_type}/{tag}', SquadsInfoByTag())
|
||||
|
||||
if __name__ == '__main__':
|
||||
model.open_model()
|
||||
import waitress
|
||||
import os
|
||||
application.add_static_route('/js', os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static'), 'js'))
|
||||
|
Loading…
x
Reference in New Issue
Block a user