CodingRat CodingRat - 7 months ago 83
Android Question

How to check database on not rooted android device

I am developing an app where i am using sqllite3 database to store values. I have Nexus S and Nexus 7 both are unrooted devices. How can i get the database for my app for debugging purpose.

I have tried
(1) I have tried all approach mentioned here

adb shell
run-as \
cp /data/data/ /sdcard/
adb pull /sdcard/application.sqlite ~/

This says cp not found..


adb -s emulator-5554 shell
# sqlite3 /data/data/
SQLite version 3.3.12
Enter ".help" for instructions
.... enter commands, then quit...
sqlite> .exit


You can write your database to the external memory with the following:

private void writeToSD() throws IOException {
    File sd = Environment.getExternalStorageDirectory();

    if (sd.canWrite()) {
        String currentDBPath = DB_NAME;
        String backupDBPath = "backupname.db";
        File currentDB = new File(DB_PATH, 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());

Where DB_NAME is the name of my database and DB_PATH is defined as follows:

        DB_PATH = context.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
    else {
        DB_PATH = context.getFilesDir().getPath() + context.getPackageName() + "/databases/";

And add the following permission (Thanks to @Sathesh for pointing this out):

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I call this method anytime I have a database write so that my most current database file is in the external memory and I can view it and debug from there.

Then you can use the X-Plore app to view the database from the external memory right on the Android device.