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?