如何使用密码从 Qt 访问 Sqlite 数据库?

How to Sqlite database with password access from Qt?

我通过 sqlitestudio SQLSicpher 创建了一个密码 sqlite 数据库文件,我如何从 qt 访问数据库?,我试过了

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

但是无法打开数据库。

Qt 不正式支持 SQLCipher,但有几个项目在它们之间创建驱动程序 QtCipherSqlitePlugin,要安装它,我使用以下命令:

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

注意:如果您使用 windows,则必须根据您的配置使用 jom, nmake or mingw32-make

该库提供了一个 example project,其中 main.cpp 如下:

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