20


I've been reading, browsing, searching a lot on this, I've criss-crossed stackoverflow back and forth many times and I managed to narrow my problem as much as I could.

The only thing I do not understand, is how to fully use an in-memory SQLite database.

Here is my situation - I have an encrypted SQLite database which I decrypt during the loading of my application (this part works for sure). My class that interacts with the database works for sure with a plain database. So to make it short, everything is flawless with a plain DB which gets loaded from the internal phone memory, but I am not sure how or where to store the decrypted DB in memory so it would get interpreted as normal DB.

I guess I should put null instead of a name in super(context, null, null, 3); and use :memory: instead of a path in SQLiteDatabase.openDatabase(), but I still don't understand fully. It says it cannot find an android_metadata table, but I am certain the database is as it should be.

Hope I was clear on this :)

Grandpa
  • 209
  • 1
  • 2
  • 5
  • Tried [SQLCipher](http://sqlcipher.net/sqlcipher-for-android/)? – Jens Jul 25 '12 at 19:59
  • I haven't given much thought. I kinda didn't find it when I first was researching, so I developed my own 2-step "encryption". Thanks for the link, I'll take a look. Can you tell me how secure is SQLCipher and does it compress data? – Grandpa Jul 25 '12 at 21:11
  • 256-bit AES encryption is ok - and you're not likely to make a better implementation on your own I'd wager. – Jens Jul 26 '12 at 07:41

3 Answers3

32

SQLiteOpenHelper() will create an in-memory database if the name is null. Note that it will be created when you invoke getWritableDatabase().

Then you should insert your data.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
12

You (or the framework) create a database using ONE of the following:

  • SQLiteDatabase.create()
  • SQLiteDatabase.openDatabase()
  • SQLiteDatabase.openOrCreateDatabase()

The first option is the only way to create a memory-only database, the other two open or create a database file.

Consequently, if you are using SQLiteOpenHelper() and you pass name as null, the framework calls SQLiteDatabase.create(null), so you will get a database that only lives in memory (it dies when close() is called). There is no need to also call one of the other direct methods. Instead call getReadableDatabase() or getWritableDatabase() from your helper.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • ok then, how do I use a database after I call `myDatabase = SQLiteDatabase.create(null);`? I have the decrypted DB as raw bytes in another class, how do I merge those bytes into the DB handle that was created? – Grandpa Jul 26 '12 at 09:16
  • I don't believe sqlite3 supports "opening" a :memory: database from an existing blob, so APIs in Android to do so likely don't exist either. Your best bet may be to stream your decrypted database out to a file where it can be opened with a valid path using `openDatabase()`. If you don't want to have the decrypted database persisted, you could copy the data into the memory-only version using queries/inserts and then close/remove the file version. – devunwired Aug 02 '12 at 21:37
  • @Devunwired, when you say "it dies when `close()` is called", are you referring to `SQLiteOpenHelper.close()`, or `SQLiteDatabase.close()`? Will closing a database connection (via `SQLiteDatabase.close()`) destroy the database? – sotrh Jul 10 '14 at 16:07
0

You have to be carefull wile using inmemory as your data will be lost once the db connection is lost. Make sure your db instance is not been closed

https://www.sqlite.org/inmemorydb.html