[SOLVED] QSqlDatabase::addDatabase("QSQLITE","localdb"); problem with name
-
With this code in the constructor of my mainwindow:
@
db = QSqlDatabase::addDatabase("QSQLITE","localdb");
db.setDatabaseName("../test/test.db");
if (!db.open()) {
QMessageBox::critical(0, QObject::tr("Database Error"),
db.lastError().text());
}model = new QSqlTableModel(this); model->setTable("items"); model->select(); qDebug() << "SQL LastError:" << model->lastError().text();
@
and in the destructor:
@
db.close();
QSqlDatabase::removeDatabase("localdb");
delete ui;
@output at start:
(I don't have the qmessagebox and my table view stay blank.)
SQL LastError: " Unable to find table items"output at close app:
QSqlDatabasePrivate::removeDatabase: connection 'localdb' is still in use, all queries will cease to work.If I remove the name of the connection, "localdb" and let the system use the default name, I have this:
@ db = QSqlDatabase::addDatabase("QSQLITE");
@SQL LastError: " "
and I have my datas shown in the table view... ?!?!?
No error at close.What am I doing wrong with the connection name?
I need it because this app will have 2 separates connections, one local with sqlite and another to a remote db2 server.
-
Hi,
For your first problem, you haven't included the database name in your QSqlTableModel constructor so the default connection is used. So create your model like this:
@
model = new QSqlTableModel(this,db);
@For your second problem, try to remove the database like this:
@
db.close();
db = QSqlDatabase();
db.removeDatabase("localdb");
@ -
Work for both solutions :)
I didn't know the model needed to specify the connection name :)
I understand for the remove that I need to disconnect the db variable with the database name/connection I want to delete... And seems to not have a method to disconnect so, replacing the object with a default one have the same effect...
Thank you very much :)