Preload SQLite database for Android app

By Nethru Limited (www.nethru.com)

When you are building an android app with quite an amount of data, populating the data on first run is not a smart choice. This article will tell you how to preload SQLite database for android app.

Files you need

1. SQLite Database Browser (or any other tools that can modify sqlite database)
2. Android SQLiteAssetHelper by Jeff Gilfelt

1. Prepare the database

1) Use SQLite Database Browser to create a new database file.(File>New Database)
Name the file as .db. I use “data.db” here.
2) Create the required tables and insert the data
3) Save the database and then close it.

2. The Android app

1) Put the “android-sqlite-asset-helper.jar” to the “libs” folder. Make sure it is included in the build path.
(The jar file can be downloaded from Android SQLiteAssetHelper)
2) Create a new class “DBHelper.java” (Remember to change DATABASE_NAME to your own one.)

public class DBHelper extends SQLiteAssetHelper {
    private static final String DATABASE_NAME = "data.db";
    private static final int DATABASE_VERSION = 1;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
}

3) Zip compress the database file as “data.db.zip” (use your own “database file name.zip”)
4) Put it in “assets/databases/” of your android project.
5) Done. You can get the “SQLiteDatabase” object in your Activity by

DBHelper dbHelper = new DBHelper(this);
dbHelper.getReadableDatabase();

Something more to explore

You may have noticed from DBHelper, DATABASE_VERSION is set to 1. The helper library does support upgrading database, for more details, please visit the library’s repository.

Conclusion

When getReadableDatabase() is called, the helper library will check if the database exists in the app data folder. If not, it will extract the database to there. It will also check the database version for upgrading or overwriting.
Three things it gives you:
1) easy creating an Android app with preloaded database
2) have a compressed database file resulting a smaller app size
3) still keep the database upgrade ability

5 comments

  • It might certainly be a better idea than relying on a taxi
    if you are travelling with bulky luggage.
    Alicante Airport sits 10 miles outside of the city center
    connected to fantastic modern roads linking the neighboring regions and towns, its approximately 90 kilometers away from La Manga del Mar Menor, roughly 50 minutes drive and there are excellent airport transfer companies offering quick simple
    transport to you holiday destinations. Public transportation- If you have planned to travel in public transportation,
    keep the details of their pick-up times and locations,
    hours of service, and the schedules handy.

  • But this difference is straightforward and simple to comprehend.
    You want to make sure that your website has relevant content.
    Product videos can be very useful in showing potential customers
    exactly how to use the product you are selling or to show the benefits of the product.

  • This increased the number of customers using the limo service.
    A wedding limo service is all that you need to give
    enough reasons for guests to talk about
    something. The unparalleled comfort and convenience of being driven in a limousine is hard to surpass, as well as
    enabling the occupant to arrive at the destination with a strong
    presence.

  • We preloaded data on our limousine hire app and it was an amazing success. Our sales increased dramatically

Leave a Reply

Your email address will not be published. Required fields are marked *