How do I view the SQLite database on an Android de

2019-01-01 07:58发布

This question already has an answer here:

I have a set of data in an SQLite database. I need to view the database on a device. How do I do that?

I have checked in ddms mode. The data in file explorer is empty.

20条回答
时光乱了年华
2楼-- · 2019-01-01 08:31

The best way I found so far is using the Android-Debug-Database tool.

Its incredibly simple to use and setup, just add the dependence and connect to the device database's interface via web. No need to root the phone or adding activities or whatsoever. Here are the steps:

STEP 1

Add the following dependency to your app's Gradle file and run the application.

debugCompile 'com.amitshekhar.android:debug-db:1.0.0'

STEP 2

Open your browser and visit your phone's IP address on port 8080. The URL should be like: http://YOUR_PHONE_IP_ADDRESS:8080. You will be presented with the following:

NOTE: You can also always get the debug address URL from your code by calling the method DebugDB.getAddressLog();

enter image description here

To get my phone's IP I currently use Ping Tools, but there are a lot of alternatives.

STEP 3

That's it!


More details in the official documentation: https://github.com/amitshekhariitbhu/Android-Debug-Database

查看更多
萌妹纸的霸气范
3楼-- · 2019-01-01 08:31

You can do this:

  1. adb shell
  2. cd /go/to/databases
  3. sqlite3 database.db
  4. In the sqlite> prompt, type .tables. This will give you all the tables in the database.db file.
  5. select * from table1;
查看更多
心情的温度
4楼-- · 2019-01-01 08:31

First post (https://stackoverflow.com/a/21151598/4244605) does not working for me.

I wrote own script for get DB file from device. Without root. Working OK.

  1. Copy script to directory with adb (e.g.:~/android-sdk/platform-tools).
  2. Device have to be connected to PC.
  3. Use ./getDB.sh -p <packageName> for get name of databases.

Usage: ./getDB.sh -p <packageName> -n <name of DB> -s <store in mobile device> for get DB file to this (where script is executed) directory.

I recommend you set filename of DB as *.sqlite and open it with Firefox addon: SQLite Manager.

(It's a long time, when i have written something in Bash. You can edit this code.)

#!/bin/sh
# Get DB from Android device.
#

Hoption=false
Poption=false
Noption=false
Soption=false
Parg=""
Narg=""
Sarg=""

#-----------------------FUNCTION--------------------------:
helpFunc(){ #help
echo "Get names of DB's files in your Android app.
Usage: ./getDB -h
       ./getDB -p packageName -n nameOfDB -s storagePath
Options:
   -h                                           Show help.
   -p packageName                               List of databases for package name.
   -p packageName -n nameOfDB -s storagePath    Save DB from device to this directory."
}


#--------------------------MAIN--------------------------:
while getopts 'p:n:s:h' options; do
    case $options in

        p) Poption=true
            Parg=$OPTARG;;

        n) Noption=true
            Narg=$OPTARG;;

        s) Soption=true
            Sarg=$OPTARG;;

        h) Hoption=true;;
    esac
done

#echo "-------------------------------------------------------
#Hoption: $Hoption
#Poption: $Poption
#Noption: $Noption
#Soption: $Soption
#Parg: $Parg
#Narg: $Narg
#Sarg: $Sarg
#-------------------------------------------------------"\\n
#echo $#    #count of params

if [ $Hoption = true ];then
    helpFunc
elif [ $# -eq 2 -a $Poption = true ];then #list
    ./adb -d shell run-as $Parg ls /data/data/$Parg/databases/
    exit 0
elif [ $# -eq 6 -a $Poption = true -a $Noption = true -a $Soption = true ];then #get DB file
    #Change permissions
    ./adb shell run-as $Parg chmod 777 /data/data/$Parg/databases/
    ./adb shell run-as $Parg chmod 777 /data/data/$Parg/databases/$Narg
    #Copy
    ./adb shell cp /data/data/$Parg/databases/$Narg $Sarg
    #Pull file to this machine
    ./adb pull $Sarg/$Narg
    exit 0
else
    echo "Wrong params or arguments. Use -h for help."
    exit 1;
fi

exit 0;
查看更多
初与友歌
5楼-- · 2019-01-01 08:32

step 1 Copy this class in your package

step 2 put the following code in your class which extends SQLiteOpenHelper.

 //-----------------for show databasae table----------------------------------------

public ArrayList<Cursor> getData(String Query)
{
    //get writable database
    SQLiteDatabase sqlDB =this.getWritableDatabase();
    String[] columns = new String[] { "mesage" };
    //an array list of cursor to save two cursors one has results from the query
    //other cursor stores error message if any errors are triggered
    ArrayList<Cursor> alc = new ArrayList<Cursor>(2);
    MatrixCursor Cursor2= new MatrixCursor(columns);
    alc.add(null);
    alc.add (null);


    try{
        String maxQuery = Query ;
        //execute the query results will be save in Cursor c
        Cursor c = sqlDB.rawQuery(maxQuery, null);

        //add value to cursor2
        Cursor2.addRow(new Object[] { "Success" });

        alc.set(1,Cursor2);
        if (null != c && c.getCount() > 0)
        {
            alc.set(0,c);
            c.moveToFirst();
            return alc ;
        }
        return alc;
    }
    catch(SQLException sqlEx)
    {
        Log.d("printing exception", sqlEx.getMessage());
        //if any exceptions are triggered save the error message to cursor an return the arraylist
        Cursor2.addRow(new Object[] { ""+sqlEx.getMessage() });
        alc.set(1,Cursor2);
        return alc;
    }
    catch(Exception ex)
    {
        Log.d("printing exception",ex.getMessage());
        //if any exceptions are triggered save the error message to cursor an return the arraylist
        Cursor2.addRow(new Object[] { ""+ex.getMessage() });
        alc.set(1,Cursor2);
        return alc;
    }
}

step 3 register in manifest

<activity
        android:name=".database.AndroidDatabaseManager"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar"/>

step 4

Intent i = new Intent(this, AndroidDatabaseManager.class);
startActivity(i);
查看更多
梦寄多情
6楼-- · 2019-01-01 08:36

If you are using a real device, and it is not rooted, then it is not possible to see your database in FileExplorer, because, due to some security reason, that folder is locked in the Android system. And if you are using it in an emulator you will find it in FileExplorer, /data/data/your package name/databases/yourdatabse.db.

查看更多
素衣白纱
7楼-- · 2019-01-01 08:36

try facebook Stetho.

Stetho is a debug bridge for Android applications, enabling the powerful Chrome Developer Tools and much more.

https://github.com/facebook/stetho

查看更多
登录 后发表回答