4

I want to see the contents of my database created in my app in the device I deploied it. I can use sqlite commands in shell and see in emulator but not in a real device.

Can anyone know how to do it in real device?

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
Bijesh
  • 317
  • 3
  • 11

5 Answers5

7

root is not necessary if the application is flagged a debuggable. You can use the run-as adb command.

e.g. to use sqlite :

adb shell "run-as com.your.package sqlite3 databases/database_name"

or copy and pull the database :

adb shell "run-as com.your.package cat databases/database_name > /sdcard/some_name"
adb pull /sdcard/some_name

unfortunately it seems that pre-ICS run-as has quite a few bugs, e.g. issue 20792 or 13965

bwt
  • 17,292
  • 1
  • 42
  • 60
  • thanks all for reply Phillipe thanks for your reply as well the copy and pull worked for me... – Bijesh Jan 05 '13 at 06:11
  • I'm kicking myself that I didn't find this sooner, as I just spent the last couple of hours rooting my phone but good to know! – jklp Jun 23 '13 at 04:20
  • Does not work with non rooted phones, I just tried with a cheap quality Micromax – Skynet Dec 03 '13 at 06:17
  • Is should work with non rooted phones (It's in fact the whole point of this question) but a lot of phones / android versions include a bugged `run-as` (even recently, see for example [issue 58373](https://code.google.com/p/android/issues/detail?id=58373)) – bwt Dec 09 '13 at 10:43
2

Actually your Real Device is not Rooted so you don't have a permission to access /data/data/<Application>/database/ directory.

Only one solution copy that file to external Storage .. And see it in SQLiteManager or any other SQLite tools.

user370305
  • 108,599
  • 23
  • 164
  • 151
1

I think you need a rooted device to access sqlite.

Look at this discussion.

Community
  • 1
  • 1
Michal
  • 2,074
  • 2
  • 22
  • 29
0

Try this,

You can copy your database in to your sdcard where you can see your database

   public void backup() {
        try {
            File sdcard = Environment.getExternalStorageDirectory();
            File outputFile = new File(sdcard,
                    "YourDatabase.db");

            if (!outputFile.exists()) 
                 outputFile.createNewFile(); 

            File data = Environment.getDataDirectory();
            File inputFile = new File(data,
                    "data/"+getPackageName()+"/databases/"+"YourDatabase.db");
            InputStream input = new FileInputStream(inputFile);
            OutputStream output = new FileOutputStream(outputFile);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new Error("Copying Failed");
        }
    }

Hope it helps.

vinothp
  • 9,939
  • 19
  • 61
  • 103
  • Am getting java.io.FileNotFoundException: /data/data/databases/timeline.db (No such file or directory) then i tried to list the contents of tht directory File file = new File(Environment.getDataDirectory()+"/data/databases"); File[] listOfFiles = file.listFiles(); I get nullpointer which means not able to access that folder.... – Bijesh Jan 05 '13 at 07:16
0

Try this function as

copyFile(new File("data/data/<app>/database"),new File("mnt/sdcard/....")).

public void copyFile(File filesrc, File filedst)
 {
  try
 {
  FileInputStream localFileInputStream = new FileInputStream(filesrc);
  FileOutputStream localFileOutputStream = new FileOutputStream(filedst);
  byte[] arrayOfByte = new byte[1024];
  while (true)
  {
    int i = localFileInputStream.read(arrayOfByte);
    if (i == -1)
    {
      localFileOutputStream.close();
      localFileInputStream.close();
      break;
    }
    localFileOutputStream.write(arrayOfByte, 0, i);
  }
}
catch (Exception localException)
{
  Log.d("XXX", "ExceptioncopyFile:" + localException.getMessage(), localException);
}

}

sandy
  • 3,311
  • 4
  • 36
  • 47