138

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.

Alex P.
  • 30,437
  • 17
  • 118
  • 169
user
  • 1,979
  • 4
  • 18
  • 18
  • http://stackoverflow.com/q/18471780/1778421 – Alex P. Dec 28 '16 at 17:54
  • Try `cordova-sqlite-detools`, an open source CLI used to pull a sqlite database from a connected Android device. https://www.npmjs.com/package/cordova-sqlite-devtools – Justin Lettau Jun 10 '17 at 14:37
  • 1
    I found a quick way to perform this by simply using Google Chrome and here is it's video.https://www.youtube.com/watch?v=fvkddnn9yps – Harpreet Oct 06 '17 at 13:28
  • if you need to process the raw data (adb, python & pandas) https://stackoverflow.com/a/49263211/2655092 – whoopdedoo Mar 15 '18 at 01:30
  • *** IMPORTANT *** If you are going to follow the most voted answer (by Andy Cochrane) then note that latest version of Firefox does not support SQLite Manager addon. You have to install any older versions. I am currently using Firefox v 27 and the addon is working fine. You can get the older versions of Firefox from here - https://ftp.mozilla.org/pub/firefox/releases/ Or simply use SQLite browser - http://sqlitebrowser.org/ p.s. had to add this as a comment because someone closed this question. – Rupam Das Mar 27 '18 at 19:20

19 Answers19

192

Here are step-by-step instructions (mostly taken from a combination of the other answers). This works even on devices that are not rooted.

  1. Connect your device and launch the application in debug mode.

  2. You may want to use adb -d shell "run-as com.yourpackge.name ls /data/data/com.yourpackge.name/databases/" to see what the database filename is.

Notice: com.yourpackge.name is your application package name. You can get it from the manifest file.

  1. Copy the database file from your application folder to your SD card.

    adb -d shell "run-as com.yourpackge.name cat /data/data/com.yourpackge.name/databases/filename.sqlite > /sdcard/filename.sqlite"

Notice: filename.sqlite is your database name you used when you created the database

  1. Pull the database files to your machine:

    adb pull /sdcard/filename.sqlite

This will copy the database from the SD card to the place where your ADB exist.

  1. Install Firefox SQLite Manager: https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

  2. Open Firefox SQLite Manager (Tools->SQLite Manager) and open your database file from step 3 above.

  3. Enjoy!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andy Cochrane
  • 2,409
  • 1
  • 18
  • 16
  • 1
    It worked, but I had to replace single quotes in step 2 with double quotes. It doesn't work with single quotes, but works with double quotes (at least on Windows). – Viachaslau Tysianchuk Mar 07 '14 at 21:10
  • 1
    adb pull /sdcard/ will pull whole sdcard. use adb pull /sdcard/filename.sqlite – Muneeb Mirza Dec 16 '14 at 08:08
  • 3
    it doesn't work on all devices (for example Nexus 9). For those I recommend this solution http://stackoverflow.com/a/29257875/291427 – kmalmur Mar 25 '15 at 14:21
  • 4
    It's also important to note that if you want to copy the DB to your SDcard, your App (the one you're "running as") needs the `android.permission.WRITE_EXTERNAL_STORAGE`-permission. Otherwise, it'll tell you "permission denied". – Lukas Knuth Jun 30 '15 at 14:10
  • You can skip step 4 by just redirecting it to a file on your local system: adb -d shell "run-as com.yourpackge.name cat /data/data/com.yourpackge.name/databases/filename.sqlite > /sdcard/filename.sqlite" > filename.sqlite – srchulo Sep 24 '15 at 22:31
  • 1
    Append a . to step 4 command to copy the file to the current directory instead of the adb programm location: `adb pull /sdcard/filename.sqlite .` – Capricorn Nov 14 '15 at 23:34
  • 12
    Just wanted to mention that I couldn't 'cat' the file to the sdcard and instead had to 'cp' it there, otherwise I would get an empty file in the sdcard. – TheIT Mar 11 '16 at 01:28
  • I use chrome app JADE for sqllite viewing. It works great – Kai Wang Jul 26 '16 at 20:49
  • 1
    Don't use `shell` parameter, use `exec-out` instead! `shell` tinkers with binary data converting `\n`s to `\r\n`s, while `exec-out` is designed for binary data transmission and won't break your DB file, copying directly to your PC like `adb -d exec-out "run-as com.example.app cat databases/example.db" 1> example.sqlite` – Pavlus Jun 07 '17 at 13:28
  • when i follow these steps and i push the database none of the tables are showing even though when i query for data while the app is running the data is there – Ankit Goel Aug 07 '17 at 00:56
  • I have an SDCard on my phone as well so im confused – Ankit Goel Aug 07 '17 at 01:01
  • To answer my own question you need to run cp instead of cat and remove the > ( pour into) – Ankit Goel Aug 07 '17 at 01:20
  • I tried these steps, able to get my db file(with .db extension) but files comes to be of size zero. Does this have to do something with the device on which I am running this ? – Avijeet Aug 23 '17 at 10:51
  • `run-as: Could not set capabilities: Operation not permitted` – deathangel908 Oct 04 '17 at 15:55
  • 1
    For me I had to use cp (copy). And as I don't have an SD Slot I wrote the data to the root of my emulated directory (you can access this with any file explorer) `adb -d shell "run-as com.example.android cp /data/user/0/com.example.android/files/sqlite.db3 /storage/emulated/0/sqlite.db3"` – Max R. Jan 03 '18 at 19:32
  • The SQLite Manager addon is not supported in latest Firefox version 59.02 .. "This add-on is not compatible with your version of Firefox." – Rupam Das Mar 27 '18 at 18:54
  • An `adb pull` will copy the full database, so you don't see the database changes in realtime (you will need to copy the database each time you change it). – Julien Kronegg Sep 21 '18 at 07:55
  • We can download a Linux SQLite browser from https://sqlitebrowser.org/ – Udara Seneviratne Oct 10 '18 at 05:56
  • I used online SQLite manager: https://sqliteonline.com/ – Gregor Ažbe Feb 25 '19 at 11:13
  • In Samsung devices shows `run-as: Could not set capabilities: Operation not permitted` – E J Chathuranga Dec 27 '19 at 05:56
86

UPDATE 2020

Database Inspector (for Android Studio version 4.1). Read the Medium article

For older versions of Android Studio I recommend these 3 options:

  1. Facebook's open source [Stetho library] (http://facebook.github.io/stetho/). Taken from here

In build.gradle:

dependencies {
  // Stetho core
  compile 'com.facebook.stetho:stetho:1.5.1'        
  //If you want to add a network helper
  compile 'com.facebook.stetho:stetho-okhttp:1.5.1'
}

Initialize the library in the application object:

Stetho.initializeWithDefaults(this);

And you can view you database in Chrome from chrome://inspect

  1. Another option is this plugin (not free)
  2. And the last one is this free/open source library to see db contents in the browser https://github.com/amitshekhariitbhu/Android-Debug-Database
Yair Kukielka
  • 10,686
  • 1
  • 38
  • 46
  • how to use Stetho in eclipse ? – Istiaque Ahmed Jun 12 '16 at 10:03
  • 1
    After entering the inspect url, I wasn't easily seeing how to access the DB. This is how - after loading the url when your app is already running, there will be a url/link with text `inspect` on the loaded page for the App. Click that link .. then to access the DB : 1) Observe the Developer Tools window pops up from browser, 2) Click the `Resources` tab, 3) The databases are under `Web SQL` in the left-hand menu – Gene Bo Nov 27 '17 at 01:50
  • 1
    The great advantage of this solution is that you also can do SQL INSERT and DELETE (which is not possible with the solutions based on `adb pull`). All all changes are reflected in realtime so you don't need to copy the database each time as with `adb pull`based solutions. – Julien Kronegg Sep 21 '18 at 07:52
  • The Db opens no doubt however, the page formatting is not up to standards. Columns are overlapped, and sluggish crawling screen. – iCantC Aug 14 '19 at 14:44
65

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

Anurag Dhunna
  • 544
  • 1
  • 5
  • 17
Bob
  • 2,430
  • 22
  • 27
54

The best way to view and manage your Android app database is to use the library DatabaseManager_For_Android.

It's a single Java activity file; just add it to your source folder. You can view the tables in your app database, update, delete, insert rows to you table. Everything from inside your app.

When the development is done remove the Java file from your src folder. That's it.

You can view the 5 minute demo, Database Manager for Android SQLite Database .

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sanath_p
  • 2,198
  • 2
  • 26
  • 22
  • Can this be used to view any database (in sqllite) or only the one created by your app? – Jasper Feb 10 '15 at 11:48
  • Only the one created by your app – sanath_p Feb 10 '15 at 23:23
  • this works with SQLiteAssetHelper as well. great tool! – Rm558 Jan 12 '16 at 01:21
  • This took me about an hour, so I hope this helps someone: if you're using DBFlow, make a class called DummySQLiteOpenHelper and put the `getData` method inside it, and get the database like this: `SQLiteDatabase sqlDB = ((AndroidDatabase)FlowManager.getWritableDatabase("Main")).getDatabase();` – androidguy Jan 23 '17 at 06:48
23

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;
Deepu
  • 598
  • 6
  • 12
  • 2
    This is working. The path is `/data/data/COM.YOUR_PACKAGE.NAME/databases` which `COM.YOUR_PACKAGE.NAME` is the name of the package you want to see its databases. Number 4 did not work, though. – Mahsa2 Aug 14 '16 at 23:57
  • Thanks Mahsa Mohammadkhani, updated step 4, as you mentioned in step 2, path is app data path. – Deepu Aug 18 '16 at 14:04
  • 5
    This works. Kinda. After step 1 you should do a 'run-as PACKAGE.NAME' otherwise you won't have read access. – Mark Gjøl May 01 '17 at 11:54
  • This is the correct answer. Ok, not the correct answer, but the simplest answer by far, and works as magic. – fiatjaf Nov 02 '17 at 19:24
  • In the step number 2, I introduce "cd /data/data/com.name.of.my.package/databases" but the result is "Permission denied". Do you have to have root access to the device? Thank you. – Alex Mar 22 '18 at 08:16
  • see the comment from – Mark Gjøl – Deepu Apr 09 '18 at 03:50
  • So I tried following : >> cd C:\Users\hp\AppData\Local\Android\Sdk\platform-tools ; >> adb shell >> cd data\data\com.myapp ... Its throwing "Permission denied" error. – pravingaikwad07 Mar 29 '20 at 12:25
16

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhijit Chakra
  • 3,201
  • 37
  • 66
7

Try AndroidDBvieweR!

  • No need for your device to be ROOTED
  • No need to import the database file of the application
  • Few configurations and you are good to go!

AndroidDBvieweR

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CLOUGH
  • 691
  • 11
  • 16
4

I have been using SQLite Database Browser to see the content SQLite DB in Android development. You have to pull the database file from the device first, then open it in SQLite DB Browser.

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
  • I need to where the database file is present.I have checked it in file explorer but data is empty.then where to get the database file. – user Oct 05 '13 at 06:14
  • Are you using emulator or real device? If on real device, then I guess DDMS won't list the internal storage (including /data). However, you can use `adb pull path_to_db_file_in_device path_to_save` to save it to computer. – Andrew T. Oct 05 '13 at 06:20
  • This only works on an emulator. – Ojonugwa Jude Ochalifu Jun 06 '15 at 07:30
  • This won't work when pulling db from a real device as it will be encrypted. If anyone knows how to use this technique with a "real" database please let me know! – niczak Jul 25 '17 at 17:32
  • it works but pulling the /data/data/your-package-name/databases/yourdb.db is cumbersone it would be nice if it would open automatically – DragonFire May 29 '20 at 07:37
4

Although this doesn't view the database on your device directly, I've published a simple shell script for dumping databases to your local machine:

https://github.com/Pixplicity/dbdump

It performs two distinct methods described here:

  1. First, it tries to make the file accessible for other users, and attempting to pull it from the device.
  2. If that fails, it streams the contents of the file over the terminal to the local machine. It performs an additional trick to remove \r characters that some devices output to the shell.

From here you can use a variety of CLI or GUI SQLite applications, such as sqlite3 or sqlitebrowser, to browse the contents of the database.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • very confused... you should explain where to put your files, and how to execute those commands... I made copy/paste in my project, open cmd and run `humpty.sh -d com.mypak.name databases/MyDB.db` it says Succes! ... but in dumps/com.mypak.name/databases folder is just a bat file witch says : `run-as: Package 'com.mypak.name' is unknown` – Choletski Aug 06 '15 at 11:46
  • @Choletski It shouldn't be outputting a batch file, that sounds like a bug. How are you executing a shell script on Windows? Through cygwin or mingw? As for extracting the database, you may want to execute the commands through adb shell yourself to see if your device supports `run-as`. – Paul Lammertsma Aug 06 '15 at 11:53
  • 1
    Works like a charm. Surprized no one upvoted this!! Thanks ! – everydayapps Oct 08 '15 at 14:47
3

Follow these steps

1>Download the *.jar file from here .

2>Put the *.jar file into the folder eclipse/dropins/ and Restart eclipse.

3>In the top right of eclipse, click the DDMS icon.

4>Select the proper emulator in the left panel.

5In the File Explorer tab on the main panel, go to /data/data/[YOUR.APP.NAMESPACE]/databases.

6>Underneath the DDMS icon, there should be a new blue icon of a Database light up when you select your database. Click it and you will see a Questoid Sqlite Manager tab open up to view your data.

*Note: If the database doesn't light up, it may be because your database doesn't have a *.db file extension. Be sure your database is called [DATABASE_NAME].db

*Note: if you want to use a DB without .db-Extension:

-Download this Questoid SqLiteBrowser: Download fro here.

-Unzip and put it into eclipse/dropins (not Plugins).

-Check this for more information Click here.

Arhat Baid
  • 1,011
  • 10
  • 18
3

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

Kai Wang
  • 3,303
  • 1
  • 31
  • 27
3

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);
Hardik Vasani
  • 876
  • 1
  • 8
  • 14
2

This works with Android 6.0 (debuggable apps at least):

adb shell "run-as your.package.name cp /data/data/your.package.name/databases/you-db-name  /sdcard/file_to_write"

Then you simply can view the DB with aSQLiteManager for instance.

lomza
  • 9,412
  • 15
  • 70
  • 85
2

You can try SQLiteOnWeb. It manages your SQLite database in the browser.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NovemberEleven
  • 245
  • 3
  • 10
2

Hope this helps you

Using Terminal First point your location where andriod sdk is loacted

eg: C:\Users\AppData\Local\Android\sdk\platform-tools>

then check the list of devices attached Using

 adb devices

and then run this command to copy the file from device to your system

adb -s YOUR_DEVICE_ID shell run-as YOUR_PACKAGE_NAME chmod -R 777 /data/data/YOUR_PACKAGE_NAME/databases && adb -s YOUR_DEVICE_ID shell "mkdir -p /sdcard/tempDB" && adb -s YOUR_DEVICE_ID shell "cp -r /data/data/YOUR_PACKAGE_NAME/databases/ /sdcard/tempDB/." && adb -s YOUR_DEVICE_ID pull sdcard/tempDB/ && adb -s YOUR_DEVICE_ID shell "rm -r /sdcard/tempDB/*"

You can find the database file in this path

 Android\sdk\platform-tools\tempDB\databases
saeed
  • 1,935
  • 1
  • 18
  • 33
1

Using file explorer, you can locate your database file like this:

data-->data-->your.package.name-->databases--->yourdbfile.db

Then you can use any SQLite fronted to explore your database. I use the SQLite Manager Firefox addon. It's nice, small, and fast.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
r4jiv007
  • 2,974
  • 3
  • 29
  • 36
1

There is TKlerx's Android SQLite browser for Eclipse, and it's fully functional alongside Android Studio. I'll recommend it, because it is immensely practical.

To install it on Device Monitor, just place the JAR file in [Path to Android SDK folder]/sdk/tools/lib/monitor-[...]/plugins.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Henrique de Sousa
  • 5,727
  • 49
  • 55
0

I found very simple library stetho to browse sqlite db of app in chrome, see

Muneesh
  • 433
  • 10
  • 19
0

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;
Community
  • 1
  • 1
t0m
  • 3,004
  • 31
  • 53