【问题标题】:Why is QSqlDatabase::drivers() empty when used in a Qt shared library project?为什么 QSqlDatabase::drivers() 在 Qt 共享库项目中使用时为空?
【发布时间】:2021-08-18 10:05:36
【问题描述】:

我正在创建一个独立的 Qt 共享库。该库基本上充当执行 SQLite 查询的接口,因此它需要使用 QSqlDatabase 模块。库的源代码:

wxsqlite3-interface.h:

#ifndef WXSQLITE3INTERFACE_H
#define WXSQLITE3INTERFACE_H

#include <QtCore/qglobal.h>

#if defined(WXSQLITE3INTERFACE_LIBRARY)
#  define WXSQLITE3INTERFACE_EXPORT Q_DECL_EXPORT
#else
#  define WXSQLITE3INTERFACE_EXPORT Q_DECL_IMPORT
#endif

extern "C" WXSQLITE3INTERFACE_EXPORT void init(const char *dbPath, const char *password);
extern "C" WXSQLITE3INTERFACE_EXPORT const char *query(const char *strQuery);

#endif // WXSQLITE3INTERFACE_H

wxsqlite3-interface.cpp:

#include "wxsqlite3-interface.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QJsonArray>
#include <QJsonDocument>
#include <QDebug>

void init(const char *dbPath, const char *password)
{
    qDebug() << "drivers" << QSqlDatabase::drivers();

    QSqlDatabase db = QSqlDatabase::addDatabase("SQLITECIPHER");
    db.setDatabaseName(dbPath);
    db.setPassword(password);
    db.setConnectOptions("QSQLITE_USE_CIPHER=rc4");
}

const char *query(const char *strQuery)
{
    QSqlQuery query(QSqlDatabase::database());
    query.exec(strQuery);

    QJsonArray result;
    while (query.next()) {
        const QSqlRecord rec = query.record();
        QJsonArray jsonRec;
        for (int i = 0; i < rec.count(); i++) {
            jsonRec << QJsonValue::fromVariant(rec.value(i));
        }
        result << jsonRec;
    }

    return QJsonDocument(result).toJson(QJsonDocument::Compact);
}

构建库后,如果我在 Qt 应用程序中运行 init 函数,一切正常。但是,如果我使用 ffi-napi 在 node.js 应用程序中运行它,则不会加载任何驱动程序。我也收到警告:

QSqlDatabase:加载驱动插件需要一个QCoreApplication实例

构建库并运行windeployqt后我的目录是:

????wxsqlite3-interface
 ┣ ????sqldrivers
 ┃ ┣ ????qsqlite.dll
 ┃ ┣ ????qsqlodbc.dll
 ┃ ┣ ????qsqlpsql.dll
 ┃ ┗ ????sqlitecipher.dll
 ┣ ????Qt5Core.dll
 ┣ ????Qt5Sql.dll
 ┗ ????wxsqlite3-interface.dll

我是否缺少一些用于非 Qt 环境中的库的 dll?

【问题讨论】:

  • 因为您从不实例化正确运行 Qt 函数/类所需的 Q(Core)Application。
  • @chehrlic 这意味着我不能在非 Qt 应用程序中使用使用 Qt 模块的库?
  • @Kazuto_Ute 很可能,驱动程序需要 QCoreApplication 提供的信息(您不需要使用事件循环)
  • @eyllanesc 我尝试在调用 QSqlDatabase::drivers() 之前在 init 函数中创建一个虚拟 QCoreApplication 实例,它摆脱了警告但仍然返回空驱动程序列表
  • 启用环境变量QT_DEBUG_PLUGINS="1"查看插件日志

标签: qt shared-libraries ffi qtsql


【解决方案1】:

如果我像这样在构建后重组目录,它会起作用:

?wxsqlite3-interface
 ┣ ?bin
 ┃ ┣ ?Qt5Core.dll
 ┃ ┣ ?Qt5Sql.dll
 ┃ ┗ ?wxsqlite3-interface.dll
 ┗ ?plugins
 ┃ ┗ ?sqldrivers
 ┃ ┃ ┣ ?qsqlite.dll
 ┃ ┃ ┣ ?qsqlodbc.dll
 ┃ ┃ ┣ ?qsqlpsql.dll
 ┃ ┃ ┗ ?sqlitecipher.dll

现在我可以通过 ffi-napi 使用 node.js 来加载 wxsqlite3-interface.dll。

我认为 Qt5Sql.dll 放在同一个文件夹中时找不到 sqldrivers 的相对路径。使用新结构,它模仿 Qt 的源文件夹并遵循 sqldrivers 到 Qt5Sql.dll 的相对路径。

虽然我不确定为什么在构建可执行文件时将 sqldrivers 文件夹放在与 Qt5Sql.dll 相同的目录中。

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2015-10-07
    • 1970-01-01
    相关资源
    最近更新 更多