Month: June 2015

Using an Existing SQLite Database with an Android Project

This article highlights how to use an Existing SQLite database with an Android Project.


A sqlite3 database is required as it is the only db that android supports (probably because it is a single simple file). Converting between databases can be tricky sometimes. In this case I needed to convert a MSSQL database to SQLite so I made use of the following repos:

Important Note Regarding ORM’s

Now you need to make a choice. Will you a using an ORM, an Object Relational Mapper. An ORM is a package that converts database records into objects. A popular approach is active record.

Some Android ORM’s:

Now if you have chosen to use an ORM take note of the following.

  • It will create the database for you based on your models and hence can’t use existing data
  • It will create tables exactly based on your models
Note: If the above is not true please comment

So basically use of an ORM is not applicable in this case as we want to ship the app with existing data.

Where is the Database Stored

The database is stored in /data/data/

So the database has to be copied over from the app to that location. We do this using the following package: Android SQLite Asset Helper. I am unaware of the ability to do this with the native Android SDK.

Where should I put the Existing SQLite file in the Android Project Structure

You need to place the database in: Project/app/src/main/assets.databases/mydb.db

Shipping your App with an existing SQLite database

First we need to add the dependency in build.gradle

dependencies {
    compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'

So do the steps above. Now we need to create a java class that uses the SQLiteAssetHelper:

public class MyDb extends SQLiteAssetHelper{
  private static final String DB_NAME = "mydb.db";
  private static final int DB_VERSION = 1;

  public MyDB(Context context){
    super(context, DB_NAME, null, DB_VERSION);

Awesome. Now to do the initial copy across. This is done when we first call getReadableDatabase().

So in or wherever you want to do this.

MyDb db = new MyDb(this);

Querying – Getting Data with a Cursor

Now to Query data and get a Cursor (not an Object) add a function to

public Cursor getData(){
      SQLiteDatabase db = getReadableDatabase();
        Cursor mCur = db.query(
                TABLE, //table
                //We Need a _id (It's required for CursorAdapter)
                null, //where clause
                null, //where parameters
                null, //groupby
                null, //having
                COLUMN_NAME + " ASC"
        return mCur;

Now for getting the data:

Cursor myCursor = db.getTrees();

Alright, so that is that. It’s not the best solution so any comments please discus below.

Root and Backup your Android Device on Ubuntu

How to Backup your Android Device on Linux


  • Android SDK Installed
  • ADB is accessible in path
  • USB cable connected to device

adb backup [-f ] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] []
                               - write an archive of the device's data to .
                                 If no -f option is supplied then the data is written
                                 to "backup.ab" in the current directory.
                                 (-apk|-noapk enable/disable backup of the .apks themselves
                                    in the archive; the default is noapk.)
                                 (-obb|-noobb enable/disable backup of any installed apk expansion
                                    (aka .obb) files associated with each application; the default
                                    is noobb.)
                                 (-shared|-noshared enable/disable backup of the device's
                                    shared storage / SD card contents; the default is noshared.)
                                 (-all means to back up all installed applications)
                                 (-system|-nosystem toggles whether -all automatically includes
                                    system applications; the default is to include system apps)
                                 ( is the list of applications to be backed up.  If
                                    the -all or -shared flags are passed, then the package
                                    list is optional.  Applications explicitly given on the
                                    command line will be included even if -nosystem would
                                    ordinarily cause them to be omitted.)

I used:

adb backup -f backup.ab -apk -obb -shared -all

You will need to accespt it on the device