Browsing your Android App's SQLite file

May 27, 2013

There is no easy way to see the contents of your app’s database in Android unless your device is rooted. If it is not rooted, you can first copy your database file to a sdcard (whether real or simulated) and then copy it back to your computer. You can then use any SQLite browser app such as Firefox’s SQLite Manager to browse the contents. These are the steps you need:

1. Add the following helper function (doesn’t matter where you put it; it’s a static function):

 1 public static void copyDatabase(String packageName, String dbName) {
 2     try {
 3         File dbFile = new File(String.format("/data/data/%s/databases/%s", packageName, dbName));
 4         if (dbFile.exists()) {
 5             File dbDestFile = new File(String.format("%s/%s", Environment.getExternalStorageDirectory().getAbsoluteFile(), dbName));
 6             dbDestFile.createNewFile();
 7             InputStream in = new FileInputStream(dbFile);
 8             OutputStream out = new FileOutputStream(dbDestFile);
 9             byte[] buf = new byte[1024];
10             int len;
11             while ((len = in.read(buf)) > 0) {
12                 out.write(buf, 0, len);
13             }
14             in.close();
15             out.close();
16         }
17     } catch (Exception e) {
18         Log.e("database_copy", e.getLocalizedMessage());
19     }
20 }
21     

2. Call the above function somewhere from your main activity:

1 
2     
3   copyDatabase(getPackageName(), "yourdatabasename.db");
4     
5 
6     

Replace “yourdatabasename.db” to the name of your own database.

3. The above 2 steps just copies your database file to your device’s sdcard. You need to copy this file now back to your computer. We’ll use adb pull to do it:

adb pull /sdcard/yourdatabasename.db ~/Desktop

Again, don’t forget to change yourdatabasename.db to the name of your own database.

The database file should now be copied to your desktop.

You can follow me on Twitter, or add me on Google+.

Discussion, links, and tweets

By day, I ship code at MetaGeek, by night, I hack on my personal projects, and finally, when I get some off time in between, I also serve as a CTO for ClockworkEngine, LLC where so far we have launched two products - Spyglass and LightPaper. Call be a serial coder if you want.

comments powered by Disqus