Stream: New Blog Post: Lions and tigers, and Pythons and Rabbits, oh my! http://bit.ly/bef5Ga Jobs: We're Hiring! >>
ReignDesign

Blog - Using your own SQLite database in Android applications

Using your own SQLite database in Android applications

Posted March 3rd, 2009 by Juan-Manuel FluxĂ 

Android Dev.Most all of the Android examples and tutorials out there assume you want to create and populate your database at runtime and not to use and access an independent, preloaded database with your Android application.

The method I'm going to show you takes your own SQLite database file from the "assets" folder and copies into the system database path of your application so the SQLiteDatabase API can open and access it normally.

1. Preparing the SQLite database file.

Assuming you already have your sqlite database created, we need to do some modifications to it.
If you don't have a sqlite manager I recommend you to download the opensource SQLite Database Browser available for Win/Linux/Mac.

Open your database and add a new table called "android_metadata", you can execute the following SQL statement to do it:

CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')

Now insert a single row with the text 'en_US' in the "android_metadata" table:

INSERT INTO "android_metadata" VALUES ('en_US')

Then, it is necessary to rename the primary id field of your tables to "_id" so Android will know where to bind the id field of your tables.
You can easily do this with SQLite Database Browser by pressing the edit table button Edit Table, then selecting the table you want to edit and finally selecting the field you want to rename.

After renaming the id field of all your data tables to "_id" and adding the "android_metadata" table, your database it's ready to be used in your Android application.

Modified database

Modified database

Note: in this image we see the tables "Categories" and "Content" with the id field renamed to "_id" and the just added table "android_metadata".

2. Copying, opening and accessing your database in your Android application.

Now just put your database file in the "assets" folder of your project and create a Database Helper class by extending the SQLiteOpenHelper class from the "android.database.sqlite" package.

Make your DataBaseHelper class look like this:

public class DataBaseHelper extends SQLiteOpenHelper{
 
    //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
 
    private static String DB_NAME = "myDBName";
 
    private SQLiteDatabase myDataBase; 
 
    private final Context myContext;
 
    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     * @param context
     */
    public DataBaseHelper(Context context) {
 
    	super(context, DB_NAME, null, 1);
        this.myContext = context;
    }	
 
  /**
     * Creates a empty database on the system and rewrites it with your own database.
     * */
    public void createDataBase() throws IOException{
 
    	boolean dbExist = checkDataBase();
 
    	if(dbExist){
    		//do nothing - database already exist
    	}else{
 
    		//By calling this method and empty database will be created into the default system path
               //of your application so we are gonna be able to overwrite that database with our database.
        	this.getReadableDatabase();
 
        	try {
 
    			copyDataBase();
 
    		} catch (IOException e) {
 
        		throw new Error("Error copying database");
 
        	}
    	}
 
    }
 
    /**
     * Check if the database already exist to avoid re-copying the file each time you open the application.
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase(){
 
    	SQLiteDatabase checkDB = null;
 
    	try{
    		String myPath = DB_PATH + DB_NAME;
    		checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
 
    	}catch(SQLiteException e){
 
    		//database does't exist yet.
 
    	}
 
    	if(checkDB != null){
 
    		checkDB.close();
 
    	}
 
    	return checkDB != null ? true : false;
    }
 
    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase() throws IOException{
 
    	//Open your local db as the input stream
    	InputStream myInput = myContext.getAssets().open(DB_NAME);
 
    	// Path to the just created empty db
    	String outFileName = DB_PATH + DB_NAME;
 
    	//Open the empty db as the output stream
    	OutputStream myOutput = new FileOutputStream(outFileName);
 
    	//transfer bytes from the inputfile to the outputfile
    	byte[] buffer = new byte[1024];
    	int length;
    	while ((length = myInput.read(buffer))>0){
    		myOutput.write(buffer, 0, length);
    	}
 
    	//Close the streams
    	myOutput.flush();
    	myOutput.close();
    	myInput.close();
 
    }
 
    public void openDataBase() throws SQLException{
 
    	//Open the database
        String myPath = DB_PATH + DB_NAME;
    	myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
 
    }
 
    @Override
	public synchronized void close() {
 
    	    if(myDataBase != null)
    		    myDataBase.close();
 
    	    super.close();
 
	}
 
	@Override
	public void onCreate(SQLiteDatabase db) {
 
	}
 
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 
	}
 
        // Add your public helper methods to access and get content from the database.
       // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
       // to you to create adapters for your views.
 
}

That's it.
Now you can create a new instance of this DataBaseHelper class and call the createDataBase() and openDataBase() methods. Remember to change the "YOUR_PACKAGE" to your application package namespace (i.e: com.examplename.myapp) in the DB_PATH string.

       ...
 
        DataBaseHelper myDbHelper = new DataBaseHelper();
        myDbHelper = new DataBaseHelper(this);
 
        try {
 
        	myDbHelper.createDataBase();
 
 	} catch (IOException ioe) {
 
 		throw new Error("Unable to create database");
 
 	}
 
 	try {
 
 		myDbHelper.openDataBase();
 
 	}catch(SQLException sqle){
 
 		throw sqle;
 
 	}
 
        ...


Enjoyed this post? ReignDesign is a great team of tech-savvy developers providing RIA and mobile services. For more articles like this, subscribe to our blog feed.

Tags: , , , ,

118 Responses to “Using your own SQLite database in Android applications”

  1. harry says:

    How can I put my larget DB( 5MB) into my android?

    @petershine & @david
    Do you solve this problem??

  2. Rodney says:

    Hi this page is really helpful. Thank you all

  3. Karl says:

    This was really helpful. Thank you.

  4. SteveC says:

    Thanks for this helpful article. Perhaps I’m missing something but shouldn’t the original assets be deleted to avoid duplicate/redundant data storage utilization?

  5. jiqqaman says:

    I dont see example on reading

  6. androiddev says:

    Thanks for this wonderful example.This is example was very helpful to me.plz help me hw can i copy large DB(30MB) from assests folder to database folder.Above example is giving problem.plz..help me

  7. Victor says:

    Thank you for your post. It was helpful so far. E encountered a problem when trying to query the DB and the application simply crashes. I only added a line at the end of your code:

    Cursor result = dbHelper.myDataBase.rawQuery(“SELECT * FROM Networks”, null);

    and it seems to crash the entire app. Everything goes well until the program reaches that line. Do I have to add any permission to the manifest file to be able to query the DB?

  8. Christina says:

    I keep getting the “no such table” error when I try to perform a select, update, or insert statement against the database. I’ve read all the comments on this page regarding this error, but none have solved the problem. Any suggestions?

  9. James says:

    How do you handle tables with an aggregate key?

  10. James says:

    Disregard…

  11. Prerna says:

    Hi When I am trying to delete the database the -id value is not resetting to 0. Hence when I add data again the values are added to the inceemented _id and Hence when I am trying to access the first row value I am not able to do so as there is no _id by that number.

  12. Nemanja says:

    I had same problem with application crash when trying to operate with database.
    I think I have a solution (at least it works for me):

    at the end of createDataBase() method I put “this.close()”.

    I think this is forcing the app to reaopen (copied) database, cause without this, it’s working with empty database
    (opened before copying with this.getReadableDataBase()) and that’s why it can’t find (existing) tables.
    Hope this helps
    Cheers

  13. Zayar says:

    It’s really useful.I put my database file (.sqlite) into assets folder. Umm, but i got error message: that file encrypted or not a database.
    I hope your help..

  14. SkyDiver says:

    That hit the spot! Excellent article!!!

    (*) My app runs with no problems even if I don’t add the extra table or change to _id.

  15. Kenny says:

    I noticed that the methods added don’t over ride or in anyway hook into the OpenHelper. Wy don’t you hook into the onCreate() over-ride?

  16. Jake says:

    Hey guys/girls,

    I was able to get this working and after reading the follow-up, thought I would post a couple things here:

    Not sure if it is the most efficient but it works without issues and was tested in HW.

    1) Reading: Get the database and set the cursor using query. This example just displays the returned database values into a listview

    SQLiteDatabase db = myDbHelper.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
    startManagingCursor(cursor);

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.fourteener_item, cursor, FROM_OUT, TO);
    setListAdapter(adapter);
    registerForContextMenu(getListView());

    2) Updating database. I would like to keep updating this pre-loaded database with fields and thus needed a way to update the database without copying it over everytime the app loaded. I created a static int representing the database version and then checked to see if was updated:

    private static final int DATABASE_VERSION = 3;

    In checkDataBase(), I added the following lines:

    if(checkDB != null){

    if (checkDB.getVersion() != DATABASE_VERSION) {
    checkDB.execSQL(“DROP TABLE IF EXISTS ” + TABLE_NAME);
    checkDB.close();
    return false;
    } else {
    checkDB.close();
    return true;
    }

    } else {
    return false;
    }

    This way, the database doesn’t get copied over everytime. However, the updated DB will replace the current one if the version
    was updated. I tried doing this by overriding onUpgrade() but could never get it to work.

    Hope this helps someone. If there is a more efficient way to do this, I would love to hear about it.

  17. Ace says:

    Hello Jake

    Could you explain how I can implement the code example you have implemented above?
    I Dont understand how you can use FROM_OUT, TO and still get the code to compile. Are these supposed to be strings?

    Does anybody know how I can query the database once I have set up as above?

  18. Jake says:

    Hello Ace,

    Those are strings.

    ORDER_BY = FIELD_NAME + ” ASC”; // also a string that states how to order the list
    private static String[] FROM_OUT = { FIELD_NAME }; // this is the field name you want to display in a listview
    private static int[] TO = { R.id.title}; // this maps to the textview which corresponds to each item in the list

    Hope this helps.

    Jake

Leave a Reply