2

i made a password sqlite database file via sqlitestudio SQLSicpher, how can i access the data base from qt?, I tried

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(dbLocation);
db.setPassword("thPassworde");

but it cannot open the database.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Zoro Allam
  • 41
  • 9

1 Answers1

1

Qt does not officially support SQLCipher but there are several projects that create a driver between them QtCipherSqlitePlugin, to install it I use the following commands:

git clone git@github.com:devbean/QtCipherSqlitePlugin.git
qmake
make
sudo make install

Note: If you are on windows you must use jom, nmake or mingw32-make depending on the configuration you have.

This library offers an example project where the main.cpp is the following:

#include <QtSql>
#include <QCoreApplication>

#ifdef Q_OS_IOS
#  include <QtPlugin>

Q_IMPORT_PLUGIN(SqliteCipherDriverPlugin)
#endif

#define CONNECTION_FAILED -1

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    Q_UNUSED(app);

    qDebug() << QSqlDatabase::drivers();
    QString dir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
//    QString DB_FILE_PATH = dir + "/test_chacha20.db";
    QString DB_FILE_PATH = dir + "/test_sqlcipher.db";
    qDebug() << "DB File Path is:" << DB_FILE_PATH;

    QSqlDatabase dbconn = QSqlDatabase::addDatabase("SQLITECIPHER");
    dbconn.setDatabaseName(DB_FILE_PATH);
    dbconn.setPassword("test");
    dbconn.setConnectOptions("QSQLITE_USE_CIPHER=sqlcipher; QSQLITE_ENABLE_REGEXP");
    if (!dbconn.open()) {
        qDebug() << "Can not open connection: " << dbconn.lastError().driverText();
        exit(CONNECTION_FAILED);
    }

    QSqlQuery query;
    query.exec("create table mapping (id int, name varchar)");
    query.exec("insert into mapping values (1, 'AAA')");
    query.exec("insert into mapping values (2, 'BBB')");
    query.exec("insert into mapping values (3, 'CCC')");
    query.exec("insert into mapping values (4, 'DDD')");
    query.exec("insert into mapping values (5, 'EEE')");
    query.exec("insert into mapping values (6, 'FFF')");
    query.exec("insert into mapping values (7, 'GGG')");
    query.exec("select * from mapping where name regexp '(a|A)$'");
    if (query.next()) {
        qDebug() << "Regexp result: " << query.value(0).toInt() << ": " << query.value(1).toString();
    } else {
        qDebug() << "This plugin does not support regexp.";
    }
    qDebug() << "----------" << endl;
    query.exec("select id, name from mapping");
    while (query.next()) {
        qDebug() << query.value(0).toInt() << ": " << query.value(1).toString();
    }
    qDebug() << "----------" << endl;
    query.exec("update mapping set name='ZZZ' where id=1");
    query.exec("select id, name from mapping");
    while (query.next()) {
        qDebug() << query.value(0).toInt() << ": " << query.value(1).toString();
    }
    qDebug() << "----------" << endl;
    query.exec("delete from mapping where id=4");
    query.exec("select id, name from mapping");
    while (query.next()) {
        qDebug() << query.value(0).toInt() << ": " << query.value(1).toString();
    }
    query.exec("drop table mapping");
    dbconn.close();

    return 0;
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • how to install it, sorry if this noob question, i need to know because i see many project that need to compile and i'm kind lose, thanks – Zoro Allam Jan 22 '19 at 06:09
  • @ZoroAllam To compile the QtCipherSqlitePlugin project I have indicated that you must follow the 4 steps – eyllanesc Jan 22 '19 at 06:13
  • @ZoroAllam whats is your OS? – eyllanesc Jan 22 '19 at 06:13
  • microsof windows 7 – Zoro Allam Jan 22 '19 at 06:14
  • @ZoroAllam okay, first download the project using the following link: https://github.com/devbean/QtCipherSqlitePlugin/archive/master.zip – eyllanesc Jan 22 '19 at 06:15
  • i dawnload the project – Zoro Allam Jan 22 '19 at 06:17
  • @ZoroAllam now unzip it and open a CMD with administrator permission located in the project folder – eyllanesc Jan 22 '19 at 06:20
  • @ZoroAllam Have you opened it as an administrator? Is it open in the same QtCipherSqlitePlugin project folder? – eyllanesc Jan 22 '19 at 06:24
  • sorry i opened the cmd for windows and typed in cd then the path of the project – Zoro Allam Jan 22 '19 at 06:27
  • @ZoroAllam okay, the execute `qmake`, then execute `nmake` – eyllanesc Jan 22 '19 at 06:28
  • it give 'nmake' is not recognized as an internal or external command operable program or batch file. – Zoro Allam Jan 22 '19 at 06:32
  • @ZoroAllam What is your compiler? – eyllanesc Jan 22 '19 at 06:32
  • i use qt 5.6 msvc 2013 – Zoro Allam Jan 22 '19 at 06:33
  • @ZoroAllam I do not remember the name of the command but you can get it easily, for it creates a project in Qt Creator and when you compile it appears some commands, then share those lines and I will tell you what the command is. – eyllanesc Jan 22 '19 at 06:36
  • this is the compiler output 08:38:44: Running steps for project manyTable... 08:38:44: Configuration unchanged, skipping qmake step. 08:38:44: Starting: "C:\Qt\Qt5.6.0\Tools\QtCreator\bin\jom.exe" C:\Qt\Qt5.6.0\Tools\QtCreator\bin\jom.exe -f Makefile.Debug 08:38:44: The process "C:\Qt\Qt5.6.0\Tools\QtCreator\bin\jom.exe" exited normally. 08:38:44: Elapsed time: 00:00. – Zoro Allam Jan 22 '19 at 06:40
  • @ZoroAllam try with `jom` or `C:\Qt\Qt5.6.0\Tools\QtCreator\bin\jom.exe` – eyllanesc Jan 22 '19 at 06:40
  • it give at the end Error 2, sorry if this take your time – Zoro Allam Jan 22 '19 at 06:45
  • @ZoroAllam I do not understand your comment, what is the error message? – eyllanesc Jan 22 '19 at 06:46
  • c\release -IC:\Qt\QT -EVE~1.0\qtbase\mkspecs\win32-msvc2013 -Fo.obj\release\ @C:\Users\zoro\AppDa ta\Local\Temp\sqlite3secure.obj.2940.62.jom jom: C:\Users\zoro\Downloads\QtCipherSqlitePlugin-master\sqlitecipher\Makefi le.Release [.obj\release\regexp.obj] Error 1 'cl' is not recognized as an internal or external command, operable program or batch file. jom: C:\Users\zoro\Downloads\QtCipherSqlitePlugin-master\sqlitecipher\Makefi le.Release [.obj\release\sqlite3secure.obj] Error 1 jom: C:\Users\zoro\Downloads\QtCipherSqlitePlugin-master\sqlitecipher\Makefi le [release-all] Error 2 – Zoro Allam Jan 22 '19 at 06:48
  • @ZoroAllam okay, I think it will be easier if Qt Creator is used, for that it deletes the decompressed folder and returned folder unzips the file, then opens the QtCipherSqlitePlugin.pro with Qt Creator, sets it in release mode and compiles it. – eyllanesc Jan 22 '19 at 06:50
  • @ZoroAllam or add `C:\Qt\Qt5.6.0\Tools\QtCreator\bin` to Environment Variables and then execute `jom` again – eyllanesc Jan 22 '19 at 06:59