我尝试重复未加载 Qt 的 QPSQL 驱动程序,但这对我没有帮助。我使用 x64 Postgresql 和我的 Qt 应用程序也是 x64
m_dataBase.addDatabase("QPSQL");
m_dataBase.setHostName(g_hostName);
m_dataBase.setPort(g_databasePort);
m_dataBase.setDatabaseName(g_databaseName);
m_dataBase.setUserName(g_userName);
m_dataBase.setPassword(g_password);
qInfo() << "drivers:" << m_dataBase.drivers();
if( !m_dataBase.isDriverAvailable("QPSQL")) {
qWarning() << "Driver QPSQL is not available";
} else {
qInfo() << "QPSQL is available";
}
if (m_dataBase.open()) {
qInfo() << "Data base open SUCCESSFUL";
return true;
} else {
qWarning() << "Data base does not open";
qInfo() << "Last ERROR:" << m_dataBase.lastError().driverText();
}
我启用了
qputenv("QT_DEBUG_PLUGINS", "1");
用于编辑调试日志。我在 cmd 中更新 PATH 变量
SET PATH=D:\PostgresLib\lib;%PATH%
SET PATH=D:\PostgresLib\bin;%PATH%
在我在输出中看到之后:
[06/05/2021 10:28:13.305] DBG line: QFactoryLoader::QFactoryLoader() looking at "C:/Qt/6.0.3/mingw81_64/plugins/sqldrivers/qsqlpsql.dll"
[06/05/2021 10:28:13.306] WAR line: Found metadata in lib C:/Qt/6.0.3/mingw81_64/plugins/sqldrivers/qsqlpsql.dll, metadata=
{
"IID": "org.qt-project.Qt.QSqlDriverFactoryInterface",
"MetaData": {
"Keys": [
"QPSQL"
]
},
"archreq": 0,
"className": "QPSQLDriverPlugin",
"debug": false,
"version": 393216
}
[06/05/2021 10:28:13.306] DBG line: Got keys from plugin meta data QList("QPSQL")
[06/05/2021 10:28:13.306] DBG line: QFactoryLoader::QFactoryLoader() checking directory path "D:/Projects/Build/sqldrivers" ...
[06/05/2021 10:28:13.309] DBG line: loaded library "C:/Qt/6.0.3/mingw81_64/plugins/sqldrivers/qsqlpsql.dll"
[06/05/2021 10:28:13.309] INF postgresdatabase.cpp virtual bool PostgresDataBase::init() line:24 drivers: QList("QSQLITE", "QODBC", "QPSQL")
[06/05/2021 10:28:13.309] INF postgresdatabase.cpp virtual bool PostgresDataBase::init() line:29 QPSQL is available
[06/05/2021 10:28:13.309] WAR postgresdatabase.cpp virtual bool PostgresDataBase::init() line:36 Data base does not open