4

Something strange happens when I try to connect to the database file in the same folder with exe. Error message is not displayed and data from the database not load.

sdb = QSqlDatabase::addDatabase("QSQLITE");
sdb.setDatabaseName("lang.sqlite");

if (!sdb.open())
{
    qDebug() << sdb.lastError().text();

    QMessageBox msgBox;
    msgBox.setIcon(QMessageBox::Critical);
    msgBox.setText(sdb.lastError().text());
    msgBox.setStandardButtons(QMessageBox::Ok);
    msgBox.exec();
}

But if I move the database to another folder - everything works fine.

sdb = QSqlDatabase::addDatabase("QSQLITE");
sdb.setDatabaseName("db\\lang.sqlite");

if (!sdb.open())
{
    qDebug() << sdb.lastError().text();

    QMessageBox msgBox;
    msgBox.setIcon(QMessageBox::Critical);
    msgBox.setText(sdb.lastError().text());
    msgBox.setStandardButtons(QMessageBox::Ok);
    msgBox.exec();
}

Any Ideas?

1 Answer 1

4

Try this:

sdb.setDatabaseName(qApp->applicationDirPath()
                    + QDir::separator()
                    + "lang.sqlite" );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.