diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 677f79a7b..46c7578bd 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -85,7 +85,6 @@ def run(args): if args.version: # we need to init sql to print the sql version # we can use an in-memory database as we just want to query the version - sql.init(':memory:') print(version.version()) sys.exit(usertypes.Exit.ok) @@ -429,7 +428,12 @@ def _init_modules(args, crash_handler): keyconf.init(qApp) log.init.debug("Initializing sql...") - sql.init(os.path.join(standarddir.data(), 'history.sqlite')) + try: + sql.init(os.path.join(standarddir.data(), 'history.sqlite')) + except sql.SqlException as e: + error.handle_fatal_exc(e, args, 'Is sqlite installed?', + pre_text='Failed to initialize SQL') + sys.exit(usertypes.Exit.err_init) log.init.debug("Initializing web history...") history.init(qApp) diff --git a/qutebrowser/misc/earlyinit.py b/qutebrowser/misc/earlyinit.py index 68f5db71e..fb09b4ab2 100644 --- a/qutebrowser/misc/earlyinit.py +++ b/qutebrowser/misc/earlyinit.py @@ -396,12 +396,6 @@ def check_optimize_flag(): "unexpected behavior may occur.") -def check_sqlite(): - from PyQt5.QtSql import QSqlDatabase - if not QSqlDatabase.isDriverAvailable('QSQLITE'): - _die('sqlite driver not available! Is sqlite installed?') - - def set_backend(backend): """Set the objects.backend global to the given backend (as string).""" from qutebrowser.misc import objects @@ -441,5 +435,4 @@ def earlyinit(args): check_libraries(backend) check_ssl_support(backend) check_optimize_flag() - check_sqlite() set_backend(backend) diff --git a/qutebrowser/misc/sql.py b/qutebrowser/misc/sql.py index 35c5359dd..4ae37f01f 100644 --- a/qutebrowser/misc/sql.py +++ b/qutebrowser/misc/sql.py @@ -37,6 +37,8 @@ class SqlException(Exception): def init(db_path): """Initialize the SQL database connection.""" database = QSqlDatabase.addDatabase('QSQLITE') + if not database.isValid(): + raise SqlException('Failed to add database. Is sqlite installed?') database.setDatabaseName(db_path) if not database.open(): raise SqlException("Failed to open sqlite database at {}: {}" @@ -50,9 +52,15 @@ def close(): def version(): """Return the sqlite version string.""" - q = Query("select sqlite_version()") - q.run() - return q.value() + try: + if not QSqlDatabase.database().isOpen(): + init(':memory:') + ver = Query("select sqlite_version()").run().value() + close() + return ver + return Query("select sqlite_version()").run().value() + except SqlException as e: + return 'UNAVAILABLE ({})'.format(e) class Query(QSqlQuery):