views:

77

answers:

1

What is the cause of following error: QSqlDatabasePrivate::database: unable to open database: unable to open database file Error opening database?

Code is 100% correct, this message appeared when I have reinstalled Windows, Python and PyQt.

EDIT: I have "read-only" flag in folder with .db file properties. It stays gray (half-checked) when I unset it and open folder properties again. I have unset "use simple sharing" flag in folder properties, I have administrator rights, there is no viruses on my computer as I can see. This nasty problem doesn't have a solution on Super User too. How to set proper permissions for sqlite for this folder and file?

+1  A: 

The above error happens when:

  • Qt considers the database valid,
  • the database isn't open, AND
  • it is not able to open the database.

You can see from the following code in src/sql/kernel/qsqldatabase.cpp (as of 4.6.2):

QSqlDatabase QSqlDatabasePrivate::database(const QString& name, bool open)
{
    const QConnectionDict *dict = dbDict();
    Q_ASSERT(dict);

    dict->lock.lockForRead();
    QSqlDatabase db = dict->value(name);
    dict->lock.unlock();
    if (db.isValid() && !db.isOpen() && open) {
        if (!db.open())
            qWarning() << "QSqlDatabasePrivate::database: unable to open database:" << db.lastError().text();                                                                                                                                

    }
    return db;
}

It looks like it's just failing to open the file. It could happen because of permissions, file location change, etc.

Kaleb Pederson