4

I want to load qt5 psql driver in windows 7.

I've load the library as this way:

qDebug() << QCoreApplication::libraryPaths ();
QString driverName = "QPSQL";
QSqlDatabase::addDatabase(driverName,"A connection to postgres");

And the output shows:

("C:/QT/Qt5.1.0/5.1.0/msvc2010/plugins", "D:/xxx/build-xxx-Desktop_Qt_5_1_0_MSVC2010_32bit-Release/release")
QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7

And in directory: C:/QT/Qt5.1.0/5.1.0/msvc2010/plugins/, there does have sqldrivers subdirectory and qsqlpsql.dll, qsqlpsqld.dll, qsqlpsqld.pdb in the subdirectory.

I load the psql library successful in ubuntu.

and my .pro file is mainly like this:

QT += core gui sql concurrent

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

unix {
LIBS += -lpython2.7 
QMAKE_CXXFLAGS += -std=c++0x
}

win32 {   
LIBS += \
    C:/Python27/libs/python27.lib 
}

I can't find the reason why it failed.

Do I need to add libpqdll.lib in .pro file? But I didn't use LIBS += -lpq in ubuntu?

BTW: I failed to build libpq in windows 7, using visual studio 2010. But this is another problem.

The question may be easy. But I've tried a wholely day.

Aylwyn Lake
  • 1,919
  • 4
  • 26
  • 36
  • If the driver is loaded as plugin, there's no need to link anything at link time, but you'll have to make sure all dependencies (libpq, check with Dependency Walker for more) are found at runtime when loading the plugin. – Frank Osterfeld Jan 02 '14 at 14:03
  • @FrankOsterfeld: Thank you for informing the Dependency Walker tool. I'll try. – Aylwyn Lake Jan 02 '14 at 14:30
  • [Similar question](http://stackoverflow.com/questions/26507353/qpsql-driver-not-loaded-qt/40558420#40558420) with other solutions. – Tarod Nov 12 '16 at 23:30

2 Answers2

7

Most likely it's failing to dynamically link the qsqlpsql.dll file when it invokes LoadLibrary on it after your program requests that driver. LoadLibrary returns the same error code for when the target DLL is missing as for when one of the DLLs the target DLL depends on is missing or cannot be loaded. So Qt quite possibly gets a "library not found" error and assumes "oh, the Pg driver must not be in this Qt build", where in fact it's present and failing to load due to dependency issues, library compatibility problems, etc.

To determine why it's failing to load, you can as Frank Osterfeld suggested use a tool like Dependency Walker (depends.exe). If you're compiling 32-bit code on a 64-bit machine you want to get the 32-bit dependency walker.

It's quite likely to be failing because there's a dependency of qsqlpsql.dll missing - either libpq.dll, or one of the libraries that libpq build requires. Dependency walker will help isolate this. It's also possible you're trying to load a 64-bit libpq.dll in a 32-bit program, or vice versa.

You may also find it informative to run your program with Process Monitor tracing it. This will produce a lot of detail about which files it attempts to open / examine, among other things, and can help track down things like unexpected PATH issues.

If you're still stuck, there's always the option of stepping through with a debugger.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • Thanks very much. Your answer is very clear. I will try to compile libpq.dll or download one if there is one appropriate. And I also require the knowledge of how to use such tools. – Aylwyn Lake Jan 03 '14 at 01:27
  • @AylwynLake You should use the *same* libpq your Qt's `qsqlpgsql.dll` was compiled against if possible. – Craig Ringer Jan 03 '14 at 01:58
-3

Try copy libmysql.dll into C:\Windows\Systems32