【问题标题】:How to Sqlite database with password access from Qt?如何使用 Qt 的密码访问 Sqlite 数据库?
【发布时间】:2019-06-15 12:28:16
【问题描述】:

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

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

但它无法打开数据库。

【问题讨论】:

    标签: c++ qt sqlite sqlcipher


    【解决方案1】:

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

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

    注意:如果您使用的是 Windows,则必须使用 jomnmakemingw32-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;
    }
    

    【讨论】:

    • 如何安装,对不起,如果这个菜鸟问题,我需要知道,因为我看到很多需要编译的项目,我有点输了,谢谢
    • @ZoroAllam 要编译 QtCipherSqlitePlugin 项目,我已经指出您必须遵循 4 个步骤
    • @ZoroAllam 你的操作系统是什么?
    • 微软视窗 7
    • @ZoroAllam 好的,首先使用以下链接下载项目:github.com/devbean/QtCipherSqlitePlugin/archive/master.zip
    猜你喜欢
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    相关资源
    最近更新 更多