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.

1 Answer 1

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 [email protected]: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;
}
Sign up to request clarification or add additional context in comments.

21 Comments

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
@ZoroAllam To compile the QtCipherSqlitePlugin project I have indicated that you must follow the 4 steps
@ZoroAllam whats is your OS?
microsof windows 7
@ZoroAllam okay, first download the project using the following link: github.com/devbean/QtCipherSqlitePlugin/archive/master.zip
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.