# Buggy code
def init(db_path):
    """Initialize the SQL database connection."""
    database = QSqlDatabase.addDatabase("QSQLITE")
    if not database.isValid():
        raise SqlError(
            "Failed to add database. Are sqlite and Qt sqlite support installed?",
            environmental=True,
        )
    database.setDatabaseName(db_path)
    if not database.open():
        error = database.lastError()
        raise SqliteError(
            "Failed to open sqlite database at {}: {}".format(db_path, error.text()),
            error,
        )


# Traceback (qutebrowser.misc.sql.SqlException)
20:42:34 DEBUG    sql        sql:run:99 Running SQL query: "SELECT EXISTS(SELECT * FROM History WHERE url = :val)"
20:42:34 DEBUG    sql        sql:run:102 query bindings: {':val': 'https://wiki.archlinux.org/index.php/ArchWiki:Privacy_policy'}
20:42:34 DEBUG    sql        debug:__exit__:264 Historycontains took 0.000158 seconds.
20:42:34 DEBUG    sql        sql:run:99 Running SQL query: "SELECT EXISTS(SELECT * FROM History WHERE url = :val)"
20:42:34 DEBUG    sql        sql:run:102 query bindings: {':val': 'https://wiki.archlinux.org/index.php/ArchWiki:About'}
20:42:34 DEBUG    sql        debug:__exit__:264 Historycontains took 0.000111 seconds.
20:42:34 DEBUG    sql        sql:run:99 Running SQL query: "SELECT EXISTS(SELECT * FROM History WHERE url = :val)"
20:42:34 DEBUG    sql        sql:run:102 query bindings: {':val': 'https://wiki.archlinux.org/index.php/ArchWiki:General_disclaimer'}
20:42:34 DEBUG    sql        debug:__exit__:264 Historycontains took 9.4e-05 seconds.
20:42:34 DEBUG    sql        sql:run:99 Running SQL query: "SELECT EXISTS(SELECT * FROM History WHERE url = :val)"
20:42:34 DEBUG    sql        sql:run:102 query bindings: {':val': 'https://wiki.archlinux.org/index.php?title=Tor&amp;mobileaction=toggle_view_mobile'}
20:42:34 DEBUG    sql        debug:__exit__:264 Historycontains took 0.000114 seconds.
20:42:34 DEBUG    sql        sql:__init__:80 Preparing SQL query: "INSERT INTO History (url, title, atime, redirect) values(:url, :title, :atime, :redirect)"
20:42:34 DEBUG    sql        sql:run:99 Running SQL query: "INSERT INTO History (url, title, atime, redirect) values(:url, :title, :atime, :redirect)"
20:42:34 DEBUG    sql        sql:run:102 query bindings: {':atime': 1501461754, ':redirect': False, ':title': 'Tor - ArchWiki', ':url': 'https://wiki.archlinux.org/index.php/Tor#Usage'}
20:42:34 ERROR    misc       crashsignal:exception_hook:210 Uncaught exception
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/history.py", line 144, in add_from_tab
self.add_url(url, title)
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/history.py", line 166, in add_url
'redirect': redirect})
File "/usr/lib/python3.6/site-packages/qutebrowser/misc/sql.py", line 214, in insert
q.run(**values)
File "/usr/lib/python3.6/site-packages/qutebrowser/misc/sql.py", line 105, in run
self.lastQuery(), self.lastError().text()))
qutebrowser.misc.sql.SqlException: Failed to exec query "INSERT INTO History (url, title, atime, redirect) values(:url, :title, :atime, :redirect)": "database is locked Unable to fetch row"

# Fixed code
def init(db_path):
    """Initialize the SQL database connection."""
    database = QSqlDatabase.addDatabase("QSQLITE")
    if not database.isValid():
        raise SqlError(
            "Failed to add database. Are sqlite and Qt sqlite support installed?",
            environmental=True,
        )
    database.setDatabaseName(db_path)
    if not database.open():
        error = database.lastError()
        raise SqliteError(
            "Failed to open sqlite database at {}: {}".format(db_path, error.text()),
            error,
        )

    # Enable write-ahead-logging and reduce disk write frequency
    # see https://sqlite.org/pragma.html and issues #2930 and #3507
    Query("PRAGMA journal_mode=WAL").run()
    Query("PRAGMA synchronous=NORMAL").run()