QSLQITE - use multiple connections SOLVED
-
hi, all,
I'm trying to open a SQLite-database connection with a specific name (The reason ist, that in my application I have to work with 2 different databases). I tried following code:
@QString connName = "Connection_to_Working_DB";
QSqlDatabase connectionDB = QSqlDatabase::addDatabase("QSQLITE", connName);
connectionDB.setDatabaseName(connString);
connectionDB.database(connName);
QString test = connectionDB.connectionName();
qDebug() << test;
QSqlQueryModel *model = new QSqlQueryModel();
model->setQuery("SELECT connection_name FROM tbl_connections");
ComboBoxConnections->setModel(model);@qDebug() gives me the correct connection name, but when I start the query, I get the message "QSqlQuery::exec: database not open"...
When I try the same code without specifying a connection name, everything works fine...
can anybody help me?
thanks in advance,
Vittorio -
You are missing the call to
@bool QSqlDatabase::open()@
Try the following:
@QString connName = "Connection_to_Working_DB";
QSqlDatabase connectionDB = QSqlDatabase::addDatabase("QSQLITE", connName);
connectionDB.setDatabaseName(connString);
if(!connectionDB.open())
QMessageBox::critical(0, "Cannot open database", connectionDB.lastError().text(), QMessageBox::Ok); @ -
Unfortunately this doesn't work... :-(
As I take it, "connectionDB.database(connName)" is an alternative way to open the database, if you want to use a specific connection name, but obviously I'm doing something wrong...
-
Hi,
It's not an alternative to open a database. It gives you the QDatabase object that represent the connection to a given database (here identified by connName) You still have to open each connection before you can use it.
-
hm, what does this mean?
@QSqlDatabase connectionDB = QSqlDatabase::addDatabase("QSQLITE", "Test");
connectionDB.setDatabaseName(connString);
connectionDB.database("Test");
connectionDB.open();@this code results in the same error, what do I overlook?!?
-
Pass db connection to setQuery method in line
@model->setQuery("SELECT connection_name FROM tbl_connections");@
like
@model->setQuery("SELECT connection_name FROM tbl_connections", connectionDB);@ -
that's it, thanks a lot!!