Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 app.package.name \
cp /data/data/package.name/databases/application.sqlite /sdcard/
exit
adb pull /sdcard/application.sqlite ~/

This says cp not found..

(2) http://developer.android.com/tools/help/adb.html#sqlite

adb -s emulator-5554 shell
# sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db
SQLite version 3.3.12
Enter ".help" for instructions
.... enter commands, then quit...
sqlite> .exit 
like image 978
CodingRat Avatar asked Jul 26 '13 14:07

CodingRat


People also ask

Which is the inbuilt database in Android?

Android comes in with built in SQLite database implementation.

How to check if my Android phone is rooted?

How to Check if My Android Phone is Rooted 1 Open Google Play, search Root Checker app to download and install it on your Android phone. 2 Open the installed Root Checker app, click "ROOT". 3 Tap on the screen tp start to check if your phone it rooted or not. Several seconds later, you can get the result. See More....

Is your phone rooted?

Well, since some phones come rooted, some folks care about how to check if your phone is rooted. As is known to all, most Android smartphones are rootable. However, there might be some risks in doing so. Be that as it may, it's still a concern to check whether your phone has been rooted and how to do it, if not.

How to recover data from unrooted Android phones?

But most Android data recovery software on the market require rooting your device which is pretty annoying. If you are one of those ones, don’t be upset, you are always able to recover missing data like WhatsApp messages, contacts, photos etc. from unrooted Android phones with Tenorshare Android Data Recovery.

How to find the root of an activity in Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken a text view. It contains information about root. Step 3 − Add the following code to src/MainActivity.java


4 Answers

The following solution works only for apps that are debuggable. It may not work well on all devices, since ​​run-as command doesn't work on some devices, especially with Jelly Bean.

  1. ​Create a *.bat file and copy the following scripts ​​

    adb shell run-as [package] chmod 777 /data/data/[package]/databases/

    adb shell run-as [package] chmod 777 /data/data/[package]/databases/[db_file_name]

    adb shell run-as [package] cp /data/data/[package]/databases/[db_file_name] /sdcard/

    adb pull /sdcard/[db_file_name]

  2. ​Change [package] to the desired application package

  3. Change [db_file_name] to the desired db name Run the bat file and you should see the copied database in the same folder as the bat file

The above solution assumes:

  • You are working on Windows
  • The device is connected and visible under "adb devices"
like image 101
kmalmur Avatar answered Oct 11 '22 12:10

kmalmur


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());
            src.close();
            dst.close();
        }
    }
}

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

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        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.

like image 38
TronicZomB Avatar answered Oct 11 '22 12:10

TronicZomB


Here's a much simple and straightforward answer: (Tested on Android one: unrooted)

adb -d shell 
$ run-as my.package.name
$ cp databases/mydatabase.db /sdcard/mydatabase.db
$ exit
$ exit

now pull your database to the default adb path

adb -d pull /sdcard/mydatabase.db

or, to your Desktop for e.g.

adb -d pull /sdcard/mydatabase.db C:\Users\user\Desktop

you may want to remove the copy with a command below:

adb -d shell "rm /sdcard/mydatabase.db"

-d option chooses the default device if having more than one emulator.

like image 11
Aung Myo Linn Avatar answered Oct 11 '22 11:10

Aung Myo Linn


If you don't know your application path then you can use this:

public void copyAppDbToExternalStorage() throws IOException {
    File sd = Environment.getExternalStorageDirectory();
    File currentDB = getApplicationContext().getDatabasePath("databaseName"); //databaseName=your current application database name, for example "my_data.db"
    if (sd.canWrite()) {
        File backupDB = new File(sd, "toDatabaseName"); // for example "my_data_backup.db"
        if (currentDB.exists()) {
            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
        }
    }
}

Or if you need copy database to public "Download" folder then you can use this:

public void copyAppDbToDownloadFolder() throws IOException {
    File backupDB = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "toDatabaseName"); // for example "my_data_backup.db"
    File currentDB = getApplicationContext().getDatabasePath("databaseName"); //databaseName=your current application database name, for example "my_data.db"
    if (currentDB.exists()) {
        FileChannel src = new FileInputStream(currentDB).getChannel();
        FileChannel dst = new FileOutputStream(backupDB).getChannel();
        dst.transferFrom(src, 0, src.size());
        src.close();
        dst.close();
    }
}

This is working perfectly on my Nexus 4 device.

like image 10
SBotirov Avatar answered Oct 11 '22 12:10

SBotirov