Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QSqlDatabase and connecting to .sqlite file

Tags:

c++

sqlite

qt

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?

like image 260
mmatviyiv Avatar asked Nov 15 '25 20:11

mmatviyiv


1 Answers

Try this:

sdb.setDatabaseName(qApp->applicationDirPath()
                    + QDir::separator()
                    + "lang.sqlite" );
like image 118
Diego Schulz Avatar answered Nov 17 '25 10:11

Diego Schulz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!