Skip to content
Cheng Liang edited this page Apr 8, 2017 · 4 revisions

Once you compiled the plugin and copy necessary files to correct path, you could use it. Usage for iOS could be found here.

Check if The Plugin Is Loaded

You could use this statement to check if the plugin is loaded successfully:

qDebug() << QSqlDatabase::drivers();

If you could find SQLITECIPHER in the output line, that means everything is OK.

Use QSqlDatabase As Usual

If the plugin is OK, you could use QSqlDatabase to operate your SQLite database as usual.

QSqlDatabase dbconn = QSqlDatabase::addDatabase("SQLITECIPHER");
// Set database name, which is the database file name in SQLite
dbconn.setDatabaseName("test.db");
// Set password if you like.
// Leave it empty if you don't want to use password.
dbconn.setPassword("password");
if (!dbconn.open()) {
    qDebug() << "Can not open connection: " << dbconn.lastError().driverText();
    exit(CONNECTION_FAILED);
}

QSqlQuery query;
// do anything you want...
...
// Don't forget to close the connection.
dbconn.close();

More Connect Options (From v0.4)

You could set more connect options using this plugin.

  • Provided by Qt SqlitePlugin
    • QSQLITE_BUSY_TIMEOUT
    • QSQLITE_OPEN_READONLY
    • QSQLITE_OPEN_URI
    • QSQLITE_ENABLE_SHARED_CACHE
  • Added by QtCipherSqlitePlugin
    • QSQLITE_CREATE_KEY
    • QSQLITE_UPDATE_KEY
    • QSQLITE_REMOVE_KEY

Use these connect options as following:

db.setConnectOptions("QSQLITE_BUSY_TIMEOUT=5000;QSQLITE_CREATE_KEY");

Encrypt an Existing Database

If you want to encrypt an existing database which has no password, use connect option QSQLITE_CREATE_KEY as while as set a password for QSqlDatabase:

QSqlDatabase dbconn = QSqlDatabase::addDatabase("SQLITECIPHER");
dbconn.setDatabaseName("test.db");
dbconn.setPassword("test");
dbconn.setConnectOptions("QSQLITE_CREATE_KEY");

Update Password

If you want to update the password of a database, use connect option QSQLITE_UPDATE_KEY as while as set the old password for QSqlDatabase and specify the new one using QSQLITE_UPDATE_KEY:

QSqlDatabase dbconn = QSqlDatabase::addDatabase("SQLITECIPHER");
dbconn.setDatabaseName("test.db");
dbconn.setPassword("test"); // the old password
dbconn.setConnectOptions("QSQLITE_UPDATE_KEY=newtest"); // set new password

Remove Password

If you want to remove the password, use connect option QSQLITE_REMOVE_KEY as while as set the password for QSqlDatabase:

QSqlDatabase dbconn = QSqlDatabase::addDatabase("SQLITECIPHER");
dbconn.setDatabaseName("test.db");
dbconn.setPassword("test");
dbconn.setConnectOptions("QSQLITE_REMOVE_KEY");
Clone this wiki locally