Database is locked. Unable to fetch row in "DETACH DATABASE..."

  • In my restless dreams I see this code:

    bool DatabaseManager::saveEncryptedDatabase(const QString &path, const QString &password)
        QSqlQuery q;
        q.exec(QString("ATTACH DATABASE '%1' AS encrypted KEY '%2'").arg(path, password));
        q.exec(QString("SELECT sqlcipher_export('encrypted')"));
        q.exec("DETACH DATABASE encrypted");
        return true;

    Where Utils::handleSqlError is printing error if any occured.

    "SQL Query Error: database encrypted is locked Unable to fetch row in\nDETACH DATABASE encrypted"

    I have even delayed it to timer and tried to detach every 250ms. No luck. I've tried with transactions. Still nothing though.
    I've got dandruff on my head.