PDA

View Full Version : Using two separate databases over ODBC inside a class fails



Tiansen
8th June 2022, 20:08
I want to use two separate databases over ODBC in my application.

If I do something like this:


db_serno=QSqlDatabase::addDatabase("QODBC");

db_serno.setDatabaseName("AccDB");

db_serno.open();
db_serno.exec(<SQL_QUERY>); <-- query performs successfully

db=QSqlDatabase::addDatabase("QODBC"); // trying to add another ODBC connection to another DB

db_serno.exec(<SQL_QUERY>); <-- query fails (last error is: Driver not loaded)

It seems that second addDatabase() somehow corrupts also the first odbc object. Obviously I am doing something wrong, how can I fix it?

Ginsengelf
9th June 2022, 07:27
Hi, the docs for QSqlDatabase::addDatabase() say:

Warning: If you add a connection with the same name as an existing connection, the new connection replaces the old one. If you call this function more than once without specifying connectionName, the default connection will be the one replaced.
So try passing a connection name as second parameter to QSqlDatabase::addDatabase().

Ginsengelf

Tiansen
9th June 2022, 22:16
Hi, the docs for QSqlDatabase::addDatabase() say:

So try passing a connection name as second parameter to QSqlDatabase::addDatabase().

Ginsengelf

Thank you, that is working!!