Question

I want to use an encrypted SQLite database in Qt. Is it possible to connect to database by QSqlDatabase and use SQLCipher to encrypt or decrypt the database? for instance regularly i connect to SQLite database file like this:

QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE","Main_Connection" );
db.setDatabaseName( "Main.sqlite" );
db.open();

I want to know is it possible to use the SQLCipher API to decrypt the database and use the Qt database module to connect and manipulate the database?

Was it helpful?

Solution

Since the API is the same, you should be able to build Qt's SQLITE driver with SQLCipher without modifications. The only caveats are,

  1. library and header file names are the same, and are available, and
  2. you initialize crypto parameters manually with the SQLCipher's PRAGMA directives before you can access any data

http://sqlcipher.net/sqlcipher-api/

If you are on Linux and cannot (or do not want to) modify system, then modify configure script under sqlite) case, such that it finds sqlcipher headers and link with correct libsqlcipher library. For example, this change allows me to build SQLite plugin with SQLCipher instead,

--- a/configure
+++ b/configure
@@ -4736,12 +4736,12 @@ for _SQLDR in $CFG_SQL_AVAILABLE; do
             if [ "$CFG_SQL_sqlite" != "no" ]; then
                 SQLITE_AUTODETECT_FAILED="no"
                 if [ "$CFG_SQLITE" = "system" ]; then
-                    if [ -n "$PKG_CONFIG" ] && $PKG_CONFIG --exists sqlite3 2>/dev/null; then
+                    if [ -n "$PKG_CONFIG" ] && $PKG_CONFIG --exists sqlite3override 2>/dev/null; then
                         QT_CFLAGS_SQLITE=`$PKG_CONFIG --cflags sqlite3 2>/dev/null`
                         QT_LFLAGS_SQLITE=`$PKG_CONFIG --libs sqlite3 2>/dev/null`
                     else
-                        QT_CFLAGS_SQLITE=
-                        QT_LFLAGS_SQLITE="-lsqlite3 -lz"
+                        QT_CFLAGS_SQLITE="-I/usr/include/sqlcipher"
+                        QT_LFLAGS_SQLITE="-lsqlcipher -lz"
                     fi
                     if compileTest unix/sqlite "SQLite" $QT_LFLAGS_SQLITE $QT_CFLAGS_SQLITE; then
                         if [ "$CFG_SQL_sqlite" = "auto" ]; then

Then you need to configure with -system-sqlite

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top