San
San

Reputation: 2088

How to view the databases in Android Device?

I need to view the database in my android device but as it an un-rooted device, I am unable to do so. I have implemented the following code in my DBAdapter class and am calling it at another activity.

CODE :

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

            if (sd.canWrite()) {
                String currentDBPath = "//data//" + "com.example.facebook_integration"
                        + "//databases//" + "image_database.db";
                String backupDBPath = "//sdcard//FBDatabase";
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
                //Toast.makeText(getApplicationContext(), "Backup Successful!",Toast.LENGTH_SHORT).show();

            }
        } catch (Exception e) {

            //Toast.makeText(getApplicationContext(), "Backup Failed!", Toast.LENGTH_SHORT).show();

        }
    }

But the above code doesn't seem to work. I have already surfed well in Stackoverflow about this question(there are quiet a few) and also tried the adb commands but none of them has worked. Can you please tell me where am going wrong at the above code or an alternative to the above solution ? Help will be much appreciated guys.

Upvotes: 1

Views: 131

Answers (3)

M D
M D

Reputation: 47807

DDMS you just select Database file and then export into your system.

Upvotes: 2

nikvs
nikvs

Reputation: 1090

You are not getting the output as your file backupDB does not physically exists. File backupDB = new File(sd, backupDBPath); - this will only create a logical file object, it may not be physically present.So you should call,

backupDB.createNewFile();

in order to physically create a file.

Upvotes: 1

M D
M D

Reputation: 47807

You can dump DB into SDcard and open into SQLite Manager o SQLite Database Tools:

    File f=new File("/data/data/yourapppackagename/databases/database");
                    FileInputStream fis=null;
                    FileOutputStream fos=null;

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

Upvotes: 2

Related Questions