Sqlite database on your Android Emulator

I started to look into using databases with Android for my first application and searching high and low over the internet, this is what I’ve compiled. One may ask where does the sqlite database of the emulator is being stored on the hard disk of your PC, well, the files ystem of the emulator does not map to a directory on your hard drive. It’s stored as an image.

Sqlite database on your Android emulator

1.Create it in onCreate() when your app first launched:

According to Using Databases | Android developer:

The recommended method to create a new SQLite database is to create a subclass of SQLiteOpenHelper and override the onCreate() method, in which you can execute a SQLite command to create tables in the database

There’s a sample coding on how to populate the DB programmatically here at the Data Storage| Android developer.

2. Create the sqlite database on your PC and adb push it to your emulator.

- Use the Android Debug Bridge (adb) pull/push to retrieve a copy of the database on your   Android emulator, populate it and put it back on the emulator.

  • Fire up your emulator.
  • Open up your command shell (Start -> Run -> type in cmd):

adb pull

Your database will be stored in data/data/<package name>/databases/<db name of your choice>

The full command as the screenshot above is:

adb pull data/data/com.chien.programmersquote/databases/quote local_database

‘quote’ is the DB that is stored on the emulator and obviously local_database is the DB file name I will be downloading my database to at C:\users\Sted\

After that you could manipulate the DB using one of the tools available freely, namely the firefox addon, Sqlite Database BrowserSqliteSpy.

In the emulator you can access these database files directly; when connecting to a real phone however you will need root access to access the database files directly.

3. Import/Export via DDMS from Eclips

Inside Eclipse while the emulator is running, you can see your file hierarchy by going to Window -> Show View -> Other.. -> Android -> File Explorer ( or just click on the DDMS icon)

ddms

It took me some time to figure out the there are 3 buttons here on the DDMS page that serves as adb pull, adb push and delete file.  Navigate to your database at data/data/<package name>/databases/ and just click on the button to either download / upload / delete the database.

Hope it helps. Ways described above are just generally some ways to deal with populating the database on your emulator for testing purposes.

This entry was posted in Android. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

9 Comments

  1. rohan
    Posted February 5, 2011 at 10:22 pm | Permalink

    thank you very much.
    Before reading this i was pushing and pulling files from the command line and it was such a waste of time.
    Sometimes so small and unnoticeable things can be of immense importance.
    thanks again for this post.

  2. Reshma
    Posted March 8, 2011 at 8:03 am | Permalink

    I am new for Android. I have created one app and local database using sqlite database browser. My database path on eclipse is MyAppName/assets/dbfile. I tried your mentioned step 3 to push this database into my emulator. My question is when I select path from DDMS like data/data// I see no databases folder in it, so does this databases folder is created default or I need to create it?

    • Posted March 8, 2011 at 9:07 am | Permalink

      The database should be in /data/data/ /databases/
      Your database should be there once you import it ( via DDMS or adb push ).
      Note that this is only for testing purposes on local environment.

    • jong
      Posted July 21, 2011 at 5:49 pm | Permalink

      just for correction. /data/data/*name of your package*/databases/

      for the sake of future viewers. XD

    • Narendra
      Posted November 27, 2011 at 6:57 pm | Permalink

      Hey Reshma, you can create database once by running your application containing code for creating the database. As soon as you run that, database will be created and you will get database folder in data/data/package_name/

  3. Nate Guerin
    Posted March 28, 2011 at 5:56 am | Permalink

    Gracias amigo. This is very helpful information.

    -Nate

  4. Posted April 4, 2011 at 7:29 pm | Permalink

    really its east than cmd method…i got it easy .. thanks:)

  5. Prabhu
    Posted October 14, 2011 at 2:00 pm | Permalink

    Simple things may help great

  6. Kasabugu
    Posted December 9, 2011 at 8:57 am | Permalink

    Thanks for info…. but my database disappears each time I close and re-open the AVD so I have to re-create it each time. I used SQLite Manager add-on for Firefox to create it. Am I doing something wrong? Also if I create the database in the /data/data/ package name/databases/ on the emulator, will it move to the SD card when it ids enabled in the manifest on loading to an actual device?

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>