No implementation found for void net.sqlcipher.database.SQLiteDatabase
Asked Answered
R

2

10

I saw this error when trying to use sqlcipher in my project. I looked it up and found several people resolved it by adding SQLiteDatabase.loadLibs(); However, it says it's expecting an @NotNull Context context and I wasn't sure what it means. Has anyone resolved this issue? This and this are two of the sources I used.

The dependency in my gradle.build is compile 'net.zetetic:android-database-sqlcipher:3.3.1-1@aar' and since I have this, it means I don't have to manually move any files to my libs directory, right?

@Override
public void onCreate(SQLiteDatabase db) {
    SQLiteDatabase.loadLibs();

    db.execSQL(CREATE_SCRIPT);
}

Apologies in advance if these are basic questions.

Reclusion answered 18/10, 2015 at 17:52 Comment(0)
D
17

However, it says it's expecting an @NotNull Context context and I wasn't sure what it means.

It means that loadLibs() needs a Context as a parameter.

Duel answered 18/10, 2015 at 17:53 Comment(11)
I am just not sure where the Context would come from, since I can't just pass a Context context as a parameter > >Reclusion
@Spider: Context is a Java class. Subclasses of Context include Activity and Service. Somewhere, an Activity or a Service is what is triggering your work with the database. For example, in this sample app, I am using an Activity to set up my SQLiteOpenHelper, and onCreate() on SQLiteOpenHelper is passed a Context as a parameter.Duel
Ohhh okay, is adding a pw to sqlite only available for paid version? Because right now, I can only getWritableDatabase with an empty string. Also, does it mean it's not encrypted? How can I look at the db I've created?Reclusion
@Spider: "Because right now, I can only getWritableDatabase with an empty string" -- SQLCipher for Android works with a non-empty passphrase, as my sample app demonstrates. "Also, does it mean it's not encrypted?" -- yes, that's the meaning of an empty database in SQLCipher.Duel
I guess what's confusing me is it looks like you just hardcoded yours (and I see it says not recommended), but is that the pw you created for your db? Can you explain to me how that works? How can I create a pw it can start using? Or is this not possible for a pre-existing database?Reclusion
@Spider: "you just hardcoded yours" -- it's a book example, so I try to keep 'em simple. "is that the pw you created for your db?" -- yes. "Can you explain to me how that works?" -- um, it's a String. I pass it to getReadableDatabase() and kin to open (and lazy-create, if needed) the database. The first time the app is run, attempting to open the database creates it, courtesy of SQLiteOpenHelper. "Or is this not possible for a pre-existing database?" -- I am uncertain what you mean by "pre-existing" here.Duel
@Spider: While my book is up to Version 6.8, Version 5.1 is available under a Creative Commons license, and it has a chapter on SQLCipher. It's for older versions of the sample (and SQLCipher for Android itself), but it may be useful to you.Duel
Oh awesome, the book I bought only covers sqlite db, but no encryption. I'll also look in there, thank you What I mean by pre-existing is that I had an app with a functioning sqlite db that was holding data, now I'm realising I should probably encrypt it and I'm trying to add that encryption to it.Reclusion
@Spider: Ah. Yes, that's a bit of a pain. I think that version of the book has a section on doing this. You basically have to temporarily have two databases: your original unencrypted one and a new encrypted one. You connect SQLCipher to both and run a few commands to slurp the data out of the unencrypted database into the encrypted one.Duel
Actually I think I found it, it seems on page 1440 you handle this scenario, although the github link no longer works. So I'm going to try that out, thank you for all your help!Reclusion
@Spider: "the github link no longer works" -- replace master with v5.1 in the URL, or just browse the repo for the v5.1 tag.Duel
C
3

Please confirm if use

SQLiteDatabase.loadLibs(CONTEXT);

where CONTEXT is the Android context

if using Fragment use getContext method else in Activity use applicationContext

Chang answered 5/8, 2019 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.