How to check database on not rooted android device

2019-01-08 08:38发布

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 

11条回答
相关推荐>>
2楼-- · 2019-01-08 08:40

I just had to do something like this and there is a way to do it, although it's a pain. You need to cat the file as the application's user account, and then pipe it to a writable location. This worked for me on my Nexus 4 running 4.3.3:

adb shell "run-as org.your.application cat /data/data/org.your.application/your-file > /mnt/sdcard/your-file"
adb pull /mnt/sdcard/your-file
查看更多
贪生不怕死
3楼-- · 2019-01-08 08:42

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.

查看更多
成全新的幸福
4楼-- · 2019-01-08 08:48

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.

查看更多
5楼-- · 2019-01-08 08:49

It isn't possible from a non-rooted phone. You cannot access the /data directory, so you can't copy the database or use sqlite app as in (2). If you need to debug, use the simulator, or from your app run queries to inspect the database.

查看更多
Viruses.
6楼-- · 2019-01-08 08:53

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.

查看更多
7楼-- · 2019-01-08 08:53

If after running

adb shell "run-as your.package.name"

you receive "run-as: Package 'your.package.name' is unknown", then try to get a database from emulator. See here: https://stackoverflow.com/a/44089632/2914140

In Android Studio 3.0 click View > Tool Windows > Device File Explorer. Expand /data/data/[package-name] nodes.

查看更多
登录 后发表回答