Sunday, May 26, 2013

Analyzing SQLite on Android

Working on an Android project with quite a sophisticated SQLite DB, I am now trying to optimize the program access to the DB, and wanted to get access to the DB file. I don't like to root my devices (maybe I am over-sensitive about that), so I used this answer by RRTW from Stack Overflow (http://stackoverflow.com/questions/4452538/android-location-of-sqlite-database-on-the-device):


File f=new File("/data/data/your.app.package/databases/your_db.db3");
FileInputStream fis=null;
FileOutputStream fos=null;

try
{
  fis=new FileInputStream(f);
  fos=new FileOutputStream("/mnt/sdcard/db_dump.db");
  while(true)
  {
    int i=fis.read();
    if(i!=-1)
    {fos.write(i);}
    else
    {break;}
  }
  fos.flush();
  Toast.makeText(this, "DB dump OK", Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
  e.printStackTrace();
  Toast.makeText(this, "DB dump ERROR", Toast.LENGTH_LONG).show();
}
finally
{
  try
  {
    fos.close();
    fis.close();
  }
  catch(IOException ioe)
  {}
}


So I can now grab the file with DDMS and examine it on my Mac.