-1

I am creating a standalone Qt shared library. This library basically acts as an interface to perform SQLite queries so it needs to use the QSqlDatabase module. The library's source code:

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);
}

After building the library, if I run the init function within a Qt application, everything works properly. However if I run it in a node.js application using ffi-napi, none of the drivers are loaded. I also get the warning:

QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins

The directory I have after building the library and running windeployqt is:

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

Is there some dlls I am missing for the library to be used in a non-Qt environment?

Kazuto_Ute
  • 679
  • 6
  • 20

1 Answers1

0

It works if I restructure the directory after building like so:

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

Now I can use node.js through ffi-napi to load wxsqlite3-interface.dll.

I think what happens is that Qt5Sql.dll can't find the sqldrivers relative path when put inside the same folder. With the new structure, it mimics Qt's source folder and follows the sqldrivers relative path to Qt5Sql.dll.

Although I am not sure why putting the sqldrivers folder in the same directory as Qt5Sql.dll works when building an executable.

Kazuto_Ute
  • 679
  • 6
  • 20