Skip to content Skip to sidebar Skip to footer

Access Database Without Rooting

I've already vainly searched extensively to find a method to access db without rooting my android phone. I don't want to use AVD as it's very slow. But in order to see what I'm doi

Solution 1:

Just add code in your application on specific event it will copy the DB into SD card, it will be copy of ur DB/data not actual DB. From SD card you can always access the DB. this is the work around but it works for me.

Here is the code

try {
        Filesd= Environment.getExternalStorageDirectory();
        Filedata= Environment.getDataDirectory();
        if (sd.canWrite()) {
            StringcurrentDBPath="data/"+sPackageName+"/databases/"+sDBName;
            StringbackupDBPath="/.appname-external-data-cache/"+sDBName; //"{database name}";Filedir=newFile(sd,backupDBPath.replace(sDBName,""));
            if(dir.mkdir()) {

            }
            FilecurrentDB=newFile(data, currentDBPath);
            FilebackupDB=newFile(sd, backupDBPath);
            if (currentDB.exists()) {
                FileChannelsrc=newFileInputStream(currentDB).getChannel();
                FileChanneldst=newFileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        } 

   } catch (Exception e) {

    }

Solution 2:

Create a function in your app to copy the DB from the internal (protected) memory to external (unprotected) such as an SD card. Then you can access it with your PC (or even an Android DB reader from the device itself if you want to).

Solution 3:

Here is the code I think you are after:

try {
                // Backup of database to SDCard
                db.open();
                File newFile = new File(Environment.getExternalStorageDirectory(), "BackupFileNameHere");
                InputStream input = new FileInputStream("/data/data/com.YourProjectName/databases/DatabaseNameHere");
                OutputStream output = new FileOutputStream(newFile);

                // transfer bytes from the Input File to the Output File
                byte[] buffer = new byte[1024];
                int length;
                while ((length = input.read(buffer))>0) {
                    output.write(buffer, 0, length);
                }
                output.flush();
                output.close();
                input.close();
                db.close();

            } catch (Exception e) {
            }

I hope this helps.

Solution 4:

We just set the file permissions to readable for all users from within the app.

if (BuildConfig.DEBUG)
{
     new File(mDB.getPath()).setReadable(true, false);
}

Then just get the .db with adb -pull

adb -d pull //data/data/xxxxx/databases/xxxxx.db .

NOTE: I've discovered that this needs to be done each time the database file is opened, for example in onCreate as well as the constructor of your SQLiteOpenHelper wrapper (when your database is not null) or perhaps onOpen. If only in onCreate, then the next time you run your app and the .db already exists, for some reason the permissions have been changed back. This might have something to do with how Android manages its data.

Post a Comment for "Access Database Without Rooting"