0

how can i pull database file from DDMS, when device is connected? I know how to pull while running in android emulator. But i want to pull it when device is connected? Please help to solve this problem.

  • Possible duplicate of [How to view the sqlite database in device android](http://stackoverflow.com/questions/19194576/how-to-view-the-sqlite-database-in-device-android) – Skynet Oct 05 '15 at 12:46
  • You will need root access on device to do it. is your device rooted? – Rahul Tiwari Oct 05 '15 at 12:54

1 Answers1

0

I think i understand your questions, you are asking that the path to grab database from android device.

we can't get some devices database, so i think possible solution is to create a backup button in your project to copy database from private folders of app and paste it to SD card. then you can access the database by using sqlite browser.

Here is the code to copy database :

// method to get backup
    public void BackupDatabase() throws IOException
    {
        try {
                File sd = new File(Environment.getExternalStorageDirectory()+"");//getBaseContext().getFilesDir();
                File data = Environment.getDataDirectory();

                if(sd == null || !sd.canWrite() ){
                    sd = getBaseContext().getFilesDir();
                }
            if (sd.canWrite())
            {
                String currentDBPath = "/data/your.package.name/databases/YourDatabaseName";
                File temp = new File(sd, "BackupFolderInSDCard");
                if(!temp.exists())
                    temp.mkdirs();

                File backupDB = new File(temp,"backup.db");
                File currentDB = new File(data, currentDBPath);

                if (currentDB.exists()) 
                {
                    FileInputStream fis = new FileInputStream(currentDB);
                    BufferedInputStream bis = new BufferedInputStream(fis,1024);

                    FileOutputStream fos = new FileOutputStream(backupDB);
                    BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);

                    byte arr[] = new byte[1024];
                    int ch;
                    while( (ch = bis.read(arr)) != -1 )
                    {
                        bos.write(arr, 0, ch);
                    }

                    bos.close(); fos.close();
                    bis.close(); fis.close();
//                  Toast.makeText(getBaseContext(), "File Saved To\n"+ backupDB.getAbsolutePath(), Toast.LENGTH_LONG).show();
                    Toast.makeText(getBaseContext(), "Database Stored in SD Card", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(getBaseContext(), "Database Doesn't Exist"+ backupDB.getAbsolutePath(), Toast.LENGTH_LONG).show();
                }
Akshay Paliwal
  • 3,718
  • 2
  • 39
  • 43