SQLite database
-
Hello,
I have an application that wants to read/write data from an SQLite database. I load the database but it does not really load it. I cannot read any data from it with a query and it returns a zero number of tables. The code is@
// Setup database
db = QSqlDatabase::addDatabase("QSQLITE");
db.setHostName("/home/verminoz/mybase.db");
db.setDatabaseName("main");bool ok = db.open(); if (!ok) { QMessageBox::critical(this, tr("database error"),tr("Could not open database! Make sure your database exists!"),QMessageBox::Ok); } qDebug() << db.tables().count() << endl;
@
The driver is available and open() returns true. Any ideas?
Thank you in advance
-
Hi,
In my opinion you should remove if your database is on the same computer:
@db.setHostName("/home/verminoz/mybase.db");@It seems that you are using Linux but anyway it is better to make your code platform independent. Try something like this:
@
db = QSqlDatabase::addDatabase("QSQLITE");
#ifdef Q_OS_LINUX
QString path(QDir::home().path());
path.append(QDir::separator()).append("mybase.db");
path = QDir::toNativeSeparators(path);
db.setDatabaseName(path);
#else
db.setDatabaseName("mybase.db");
#endifbool ok = db.open();
if (!ok)
{
QMessageBox::critical(this, tr("database error"),tr("Could not open database! Make sure your database exists!"),QMessageBox::Ok);
}
qDebug() << db.tables().count() << endl;
@Regards,
Leon