Riddhi Dudani
Riddhi Dudani

Reputation: 107

Database is created for emulator but not for android device

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.

Upvotes: 0

Views: 1353

Answers (2)

Joby Wilson Mathews
Joby Wilson Mathews

Reputation: 11116

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);
    }


}

Upvotes: 1

Konrad Krakowiak
Konrad Krakowiak

Reputation: 12365

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.

Upvotes: 1

Related Questions