Skip to content
Advertisement

How to deal: QSqlQuery::exec() returns false

I’m using Qt and sqlite3. The problem is:

QSqlQuery::exec() returns false.

I thought it is caused by QSqlDatabase::open() because it always returns true! I found that actually does not matter what i set using QSqlDatabase::setDatabaseName(), it will return true, because sqlite will create non existing DB.

I used QFile::exist to test if DB exists. But everything is fine and QFile sees the db.

Code:

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("temp_client_db.sl3");

    QFile datebase("temp_client_db.sl3");

    if (!datebase.exists() || !db.open())
    {
       QMessageBox::critical(this, "Error", "Database isn't connected.");
    }
    else
    {
        QSqlQuery query;
        query.prepare("SELECT * FROM exist_ask");

        if (!query.exec())
        {
             QMessageBox::critical(this, "Error", ""SELECT exist_ask" works badly.");
        }
        else
        {
             ...

messagebox

Advertisement

Answer

I just did not add the database to the build directory.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement