Qt SQL driver plugin for SQLCipher ( for Qt 5 )
-
I have SQLite database in my qt app and i want to enctypt it. The one free solution is to use SQLCipher plugin for this purpose. I have found this plugin on github and i have successfully built it with MSYS2 MinGW64 following these instructions:
git clone https://github.com/sjemens/qsqlcipher-qt5.git cd c:/temp/qsqlcipher-qt5 mkdir -p build && cd build qmake ../qsqlcipher.pro make make install
What hava i to do next to use this plugin like this:
#include <QCoreApplication> #include <QSqlDatabase> #include <QSqlQuery> #include <QDebug> #include <QString> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug() << QSqlDatabase::drivers(); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLCIPHER"); db.setDatabaseName("C:/sqlcipher/encrypted.db"); db.open(); QSqlQuery q; q.exec("PRAGMA key = 'testkey';"); q.exec("insert into testtable (id,name) values(4,'dummy')"); q.exec("SELECT id,name anz FROM testtable"); while (q.next()) { QString id = q.value(0).toString(); QString name = q.value(1).toString(); qDebug() << "id=" << id << ", name=" << name; } db.close(); return 0; }
I'm doing it in Windows 10 and in QT 5.15.2
Help pls. -
Hi and welcome to devnet,
What exactly is the question ?
In any case, you should add error checking to your SQL queries calls. -
@SGaist Hi, thanks for answer.
when i run this program i'm getting this app output:QSqlDatabase: QSQLCIPHER driver not loaded QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7 Cannot open database: QSqlError("", "Driver not loaded", "Driver not loaded")
What should i do to get QSQLCIPHER driver loaded? I tried to move the containing of C:\temp\qsqlcipher-qt5-5.15-2\3rdparty in C:\Qt\5.15.2\Src\qtbase\src\3rdparty directory, containing of
C:\temp\qsqlcipher-qt5-5.15-2\qsqlcipher in C:\Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers directory, and containing of C:\temp\qsqlcipher-qt5-5.15-2\build\qsqlcipher\plugins\sqldrivers
in C:\Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers directory (files libqsqlcipher.dll.a, qsqlcipher.dll, qsqlcipher.dll.debug). But it doesn't work anyway. You can check containing of these folders (except build) in github. -
@BB44
If it didn't solve the problem. I did so. I am using qt 6.5.0. But, I think, there will be no difference if you use qt 5 version. I compiled the sqlcipher dll library through vcpkg, but I couldn't build it from the sources. I created my own sqlcipher driver class using the source code of the sqlite driver, which is in the sources, changed only the name of the driver class and the paths inside the source files, added the SqlDriverCreator class, registered the new driver in the code:sqldrvcreator = new SqlDriverCreator; QSqlDatabase::registerSqlDriver("SQLITECIPHER", sqldrvcreator);,
After that everything works. You can also create a plugin project and build the plugin dll library and drop one dll file into the release folder with the project in the release_dir\plugins\sqldrivers folder and it should work without registering the driver and including libraries in the pro file.