diff --git a/mendeleev/db.py b/mendeleev/db.py index 030706df..b2ce8831 100644 --- a/mendeleev/db.py +++ b/mendeleev/db.py @@ -13,15 +13,21 @@ def get_package_dbpath() -> str: return os.path.join(os.path.abspath(os.path.dirname(__file__)), DBNAME) -def get_engine(dbpath: str = None) -> Engine: +def get_engine(dbpath: str = None, read_only: bool = True) -> Engine: """Return the db engine""" if not dbpath: dbpath = get_package_dbpath() - return create_engine("sqlite:///{path:s}".format(path=dbpath), echo=False) + if read_only: + connectstr = "sqlite:///file:{path:s}?mode=ro&nolock=1&uri=true".format( + path=dbpath + ) + else: + connectstr = "sqlite:///{path:s}".format(path=dbpath) + return create_engine(connectstr, echo=False) -def get_session(dbpath: str = None) -> Session: +def get_session(dbpath: str = None, read_only: bool = True) -> Session: """Return the database session connection.""" - engine = get_engine(dbpath=dbpath) + engine = get_engine(dbpath=dbpath, read_only=read_only) db_session = sessionmaker(bind=engine, autoflush=False, autocommit=False) return db_session()