0

I am making an android app in which database is created with sqlite open helper. when I installed my app in emulator it works fine but when I did the same thing to test in my phone I don't see any database created in DDMS file explorer view data/data. It is coming out to be empty. Please help me out with this.

Thanks.

Riddhi Dudani
  • 107
  • 1
  • 1
  • 10

2 Answers2

1

You can see on DDMS private files of application (from data/data directory) on emulator but it is not possible to see in device without root. But it is not means that database doesn't not exist. If you want to get db from real device, you have to root it or write some code in your app which copies db file on sdcard.

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
  • Thanks. That might be the reason. But I recently updated my phone to Lollipop. Before that I was able to see the database after pulling from my device in my system. Previously also it was not rooted. Does rooting harm my device? Then I would like to give it a try. – Riddhi Dudani Feb 15 '15 at 13:49
  • It is depend from device. You can lose guarantee. And if you have Samsung device with KNOX you will burn the bit. Basically the rooting of device is not good practice. If you don't have to don't do this. – Konrad Krakowiak Feb 15 '15 at 13:52
  • So you can accept my solution? If it is help you of course. – Konrad Krakowiak Feb 16 '15 at 15:41
  • Yes the database was created. I returned the getCount() method from the cursor which is getting updated everytime. That means my code is working perfectly. Thanks a lot for your help @KonradKrakowiak. – Riddhi Dudani Feb 19 '15 at 08:15
  • @RiddhiWala Did you try my solution? If it was helpful for you please accept it :) – Konrad Krakowiak Mar 17 '15 at 17:19
1

Root the android device

or

write a code for coping db file to sdcard

Code for coping database from /data/data directory to sdcard

public void copyDBToSDCard() {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//"+getPackageName()+"//databases//"+DB_NAME+"";
            String backupDBPath = "backupname.db";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }

        }
        Toast.makeText(print.this, 
                   "Database Saved", Toast.LENGTH_LONG).show();
    }  catch (Exception e) {
        Toast.makeText(print.this, 
                   "Error="+e, Toast.LENGTH_LONG).show();
        Log.i("FO","exception="+e);
    }


}
Joby Wilson Mathews
  • 10,528
  • 6
  • 54
  • 53