0

I am trying to create a PostgreSQL database with PyQt5. After creating a database using QSqlDatabase.addDatabase(), calling lastError().text() on that object returns the following:

Driver not loaded Driver not loaded

I have already tried

  • copying libpq.dll from the PostgreSQL lib folder to Lib\site-packages\PyQt5\Qt\bin in my Python folder
  • adding sqlite3 to Lib\site-packages\PyQt5\Qt\bin in my Python folder
  • adding the PostgreSQL lib and include folders to the PATH

This is the code I am using to test:

import sys
from PyQt5.QtSql import *
from PyQt5.QtWidgets import *

app = QApplication(sys.argv)
if not "QPSQL" in QSqlDatabase.drivers():
    print('QPSQL not in QSqlDatabase.drivers()')
qQslDb = QSqlDatabase.addDatabase("QPSQL")
qQslDb.setHostName('<Host>')
qQslDb.setDatabaseName('<Name>')
qQslDb.setUserName('<Username>')
ok = qQslDb.open()
if ok:
    print("OK, database is open")
else:
    print(qQslDb.lastError().text())
    exit()

Edit: lastError().text() actually only returns Driver not loaded Driver not loaded. These two lines

QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL

come from the call to QSqlDatabase.addDatabase("QPSQL") if I am not mistaken. For some reason though, when using bash to run the program these two lines are always at the very start of the command line output, even if I put a print("test") in the first line of the program. When using cmd however, it's in the "right" order.

gearmic
  • 47
  • 5
  • @AdrianKlaver C++ syntax is different to Python syntax – eyllanesc Aug 09 '20 at 16:06
  • Hmm, coffee has not kicked in. Does the code work with any of the other drivers, say 'QSQLITE'? What does ```QSqlDatabase.drivers()``` return? – Adrian Klaver Aug 09 '20 at 16:12
  • @AdrianKlaver ```QSqlDatabase.drivers()``` returns ```['QSQLITE', 'QODBC', 'QODBC3', 'QPSQL', 'QPSQL7']```. I tried following the [link=instructions](https://doc.qt.io/qt-5/sql-driver.html#how-to-build-the-qpsql-plugin-on-windows) to build QPSQL, but I seem to be missing qmake (maybe because I installed qt using pip?). Same problem with the other listed drivers. – gearmic Aug 09 '20 at 19:02
  • ```QSqlDatabase.drivers()``` is returning drivers so they are there and discoverable. I don't see that you have to build them. The issue seems to be that the drivers are seen but not loaded. Not sure yet why? – Adrian Klaver Aug 09 '20 at 19:23
  • Is there a way you can the errors before ```lastError()```? – Adrian Klaver Aug 09 '20 at 19:37
  • The [QSqlDatabase Class](https://doc.qt.io/qt-5/qsqldatabase.html) apparently doesn't have any other function to retrieve errors other than lastError() and that is pretty much being called as early as possible in my test program. Also I forgot to add that in the folder Lib\site-packages\PyQt5\Qt\plugins\sqldrivers (within the Python folder) there are actually three files already, that have similar names to the listed drivers: qsqlite.dll, qsqlodbc.dll and qsqlpsql.dll. – gearmic Aug 09 '20 at 21:30
  • Searching for other answers to this on SO found that it is often a dependency issue. Where ```qsqlpsql.dll``` is not finding ```libpq.dll```. The suggestion is to use [Dependency Walker](http://www.dependencywalker.com/) to see if that is the case. – Adrian Klaver Aug 11 '20 at 20:04
  • Dependency Walker says that the modules QT5SQL.DLL and QT5CORE.DLL are missing. LIBPQ.DLL has the module warning smbol beside it. There are a few dependencies of LIPBQ that are missing. Most of them are called something like "API-MS-WIN-CORE-...". – gearmic Aug 13 '20 at 21:01

0 Answers0