A small (noob) tutorial for those wanting to use SQLCipher programatically (calling sqlite3_key instead of PRAGMA) in their C projects (in the mean time I’ve managed to overcome the lack of doc).
#Building SQLCipher from source
(a small update to README.md is necessary, an update will be suggested on the official git page):
- $ git clone https://github.com/sqlcipher/sqlcipher.git
- $ cd sqlcipher
- $ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2" LDFLAGS="-lcrypto"
- $ make
- $ make install #if you want to do a system wide install of SQLCipher
Mark the output of make install, especially the following lines:
- libtool: install: /usr/bin/install -c .libs/libsqlcipher.a /usr/local/lib/libsqlcipher.a
- /usr/bin/install -c -m 0644 sqlite3.h /usr/local/include/sqlcipher
these are the folders of SQLCipher headers and the library necessary when building your proper C project.
#Building your own minimal C project example
In your SQLite_example.c put the following lines (source file available at http://goo.gl/tGWDum):
#include “sqlite3.h” //We want to SQLCipher extension, rather then a system wide SQLite header
rc = sqlite3_open(“test.db”, &db); //open SQLite database test.db
rc = sqlite3_key(db, “1q2w3e4r”, 8);//apply encryption to previously opened database
Build your example:
- $gcc SQLite_example.c -o SQLtest -I /path/to/local/folder/with/sqlcipher/header/files/ -L /path/to/local/folder/with/sqlcipher/library.a -l sqlcipher
e.g. with paths extracted from the output of $make install
- $gcc SQLite_example.c -o SQLtest -I /usr/local/include/sqlcipher -L /usr/local/lib/libsqlcipher.a -lsqlcipher
Finally, make sure that your SQLCipher library is in the system wide library path e.g. for (Arch)Linux:
- $ export LD_LIBRARY_PATH=/usr/local/lib/
Run your test code ((Arch)Linux):