solve problem with web and sqlite3.DatabaseError: database disk image is malformed

This commit is contained in:
norohind 2021-12-18 22:22:15 +03:00
parent 923b5d35e9
commit d2076894d4
Signed by: norohind
GPG Key ID: 01C3BECC26FB59E1
2 changed files with 12 additions and 16 deletions

View File

@ -3,6 +3,7 @@ import sqlite3
import utils import utils
from . import sqlite_sql_requests from . import sqlite_sql_requests
import json import json
import os
from typing import Union from typing import Union
from datetime import datetime from datetime import datetime
@ -10,23 +11,22 @@ from datetime import datetime
class SqliteModel: class SqliteModel:
db: sqlite3.Connection 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: :return:
""" """
self.db = sqlite3.connect('squads.sqlite', check_same_thread=False) db = sqlite3.connect(f'file:{os.environ["SQLITE_DB"]}?mode=ro', check_same_thread=False, uri=True)
self.db.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r)) 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.create_function('null_fdev', 1, self.null_fdev, deterministic=True)
def close_model(self) -> None: return db
"""
This method should be called before program exit
:return:
"""
self.db.close()
@staticmethod @staticmethod
def null_fdev(value): def null_fdev(value):

View File

@ -9,8 +9,6 @@ from EDMCLogging import get_main_logger
logger = get_main_logger() logger = get_main_logger()
logger.propagate = False logger.propagate = False
model.open_model()
class SquadsInfoByTagHtml: class SquadsInfoByTagHtml:
def on_get(self, req: falcon.request.Request, resp: falcon.response.Response, tag: str, details_type: str) -> None: 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 = AppFixedLogging()
# application = falcon.App()
application.add_route('/squads/now/by-tag/{details_type}/{tag}', SquadsInfoByTagHtml()) application.add_route('/squads/now/by-tag/{details_type}/{tag}', SquadsInfoByTagHtml())
application.add_route('/api/squads/now/by-tag/{details_type}/{tag}', SquadsInfoByTag()) application.add_route('/api/squads/now/by-tag/{details_type}/{tag}', SquadsInfoByTag())
if __name__ == '__main__': if __name__ == '__main__':
model.open_model()
import waitress import waitress
import os import os
application.add_static_route('/js', os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static'), 'js')) application.add_static_route('/js', os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static'), 'js'))