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.