how to add SQLcipher to android project?

129 views Asked by At

Im trying to use SQLCipher to encrypt my database for my project.ive added all the assets and libs requires for using sqlCipher but the problem is that i have an entire class dedicated to just database operations so im a bit confused of where im supposed to be adding the required code as mentioned here. i did try initializing the said libraries in my database open() method but that just gave me a no such table exception.

Heres my code before any modifications. Any help is gratefully accepted

public class DataBaseHandler {
public static final String KEY_ID = "id";
public static final String KEY_XCOD = "xcod";
public static final String KEY_YCOD = "ycod";
public static final String KEY_IMG = "img";
public static final String KEY_PATTERN = "pattern";
public static final String KEY_PACKAGENAME = "packagename";
private static final String DATABASE_NAME = "P3UserDb";
private static final String DATABASE_USERTABLE = "LoginDetailsTable";
private static final String DATABASE_PATTERNTABLE = "PatternDetailsTable";
private static final String DATABASE_LOCKEDAPPSTABLE = "LockedAppsTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

public static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_USERTABLE + " (" + KEY_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_XCOD
                + " INTEGER NOT NULL, " + KEY_YCOD + " INTEGER NOT NULL, "
                + KEY_IMG + " TEXT NOT NULL);");
        db.execSQL("CREATE TABLE " + DATABASE_PATTERNTABLE + " ("
                + KEY_PATTERN + " STRING NOT NULL);");
        db.execSQL("CREATE TABLE " + DATABASE_LOCKEDAPPSTABLE + " ("
                + KEY_PACKAGENAME + " STRING);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_USERTABLE);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_PATTERNTABLE);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_LOCKEDAPPSTABLE);
        onCreate(db);
    }
}

public DataBaseHandler(Context c) {
    ourContext = c;
}

public DataBaseHandler open() throws SQLException {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}
.
.
.
Other Database operations
.
.
.
}
1

There are 1 answers

0
Nick Parker On

Before you initialize your SQLiteOpenHelper subclass, you need to call SQLiteDatabase.loadLibs(…); passing it a context. That will properly allow the native libraries to load.