qtqsqldatabase

Qt QPSQL QSqlDatabase: named connection fails


I am trying to have multiple QPSQL QSqlDatabase connections in my application.

When I use only one connection - default/ no name, - it works flawlessly.

But as soon as I try to create a named connection, it fails.

The error message is: "Driver not loaded".

This works:

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");

But this fails:

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", "somestringhere");

The other properties are precisely the same: server, port, user name and password. The only difference is in the 2nd parameter of the addDatabase method.

If I go back and forth, trying with or without the 2nd input parameter, the results are consistent: the connection is always working without the 2nd parameter and always fails with it.

Am I doing something wrong, or is there a bug in QPSQL?

The app uses Qt 6.3.0 with Qt's MinGW-64 on Windows 10.


Solution

  • The root cause of the problem was in my multithreaded code.

    Once I fixed that, the database connection started working as expected.

    It is unfortunate that the error message was misleading (i.e., there was actually nothing wrong with the driver), but at least I found the problem and fixed.