1

What is the difference between using external db file(from assets) and creating a new db using SQLiteOpenHelper? Performance wise which one will be fast? will accessing db from assets slow down my application.

3 Answers 3

4

What is the difference between using external db file(from assets) and creating a new db using SQLiteOpenHelper?

You cannot directly use a database that is pre-packaged in your app as an asset. SQLite needs a file; an asset is not a file, but rather is an entry in the ZIP file that makes up an APK.

Using something like SQLiteAssetHelper, you can a pre-packaged database as the starting point for your user, by copying the database from assets into internal storage for your app.

will accessing db from assets slow down my application.

That depends on what you compare it to, and even then the difference will only be the first time you try to work with the database.

With the database-packaged-as-a-asset approach, the database needs to be copied from assets before it can be used. This takes time. It should not be dramatically slower than creating an empty database and executing SQL statements to populate it. In fact, I would expect it to be faster in many cases. However, it will be slower than starting from an empty database.

Once the database is created and set up -- whether via your own CREATE TABLE statements or by copying a starter database from assets -- performance will be identical, because the databases themselves are identical.

Sign up to request clarification or add additional context in comments.

Comments

2

The assets folder is read-only, so if you need to edit your database then it won't work for yourapp. If you have a pre-populated database, one option is to make a copy from the assets folder into somewhere that is writeable when your app is first accessed, and then continue with using the writeable version.

Comments

0

There isn't a performance difference assuming your database is properly configured. See this:

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

I'm using this procedure for a ~500mb database upon which I have to do fairly complex and substantial queries routinely, and which I zip for distribution and unzip on the first run due to size and this works flawlessly.

3 Comments

The code from that blog post is old, nasty, and flawed. SQLiteAssetHelper would be a better choice for new development: github.com/jgilfelt/android-sqlite-asset-helper
It hard-codes a path to the database file, one that will be wrong in some cases (e.g., secondary accounts on Android 4.2+ tablets). Use getDatabasePath(). It will have problems with larger databases, due to oddities in how Android treats files for compression purposes, which is why SQLiteAssetHelper requires a ZIP file. It does not sync() the disk write. And it inherits from SQLiteOpenHelper while not properly supporting SQLiteOpenHelper (e.g., extraneous openDataBase(), rather than using getReadableDatabase() and kin). There may be more, but those are a starter.
Ah, alright - I had actually adressed those in my implementation. Much appreciate the writeup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.