Ojonugwa Jude Ochalifu
Ojonugwa Jude Ochalifu

Reputation: 27237

View SQLite database on device in Android Studio

I am using the latest version of Android Studio. When I run my app on the emulator, I am able to view my database by going through:

tools -> Android Device Monitor -> clicking on the emulator in the left panel -> file explorer -> data -> data -> com.project-name

But this option isn't available when running my app on a device.

I have checked related questions:

  1. Android - viewing SQLite databases on device? The most voted answer here suggests copying the database to SDcard, and it's even for eclipse.
  2. Access sqlite database on android device

and these questions are from 2011 and 2010. Are there any plugins I can use or other external tools?

Upvotes: 23

Views: 62887

Answers (5)

vini b
vini b

Reputation: 49

There's a new native way on Android Studio 4.1 version called Database Inspector, check this answer!

enter image description here

It will be shown in the bottom of android studioenter image description here

Upvotes: 2

Nabin
Nabin

Reputation: 1541

The easiest way to see realtime Database are:

#For Android Studio 4.1 Canary 6 and higher

You can use a very simple Android Studio's feature Database Inspector. Where you can inspect, query, and modify your app’s databases using the new Database Inspector. For example, you can debug your running app by modifying values in your database and testing those changes on the device in real-time without leaving Android Studio.

To get started, deploy your app to a device running API level 26 or higher and select View > Tool Windows > Database Inspector from the menu bar.

#For Android Studio 4.0 and lesser

Use the Android Debug Database library

enter image description here

  • Android Debug Database allows you to edit, delete, create the database and shared preferences values directly in your browser in a very simple way
  • Search in your data
  • Debug Room in-memory database
  • No need to root device

Using:


Add this to your app's build.gradle

debugImplementation 'com.amitshekhar.android:debug-db:1.0.6'

Launch/Run app


Find the debugging link in logs in LogCat

i.e. D/DebugDB: Open http://192.168.232.2:8080 in your browser

the link will be different and open it in the any browser

That's it Enjoy!!!

  • If you are using it over USB, run ADB forward tcp:8080 tcp:8080
  • Android phone and laptop should be connected to the same Network

For more details go to the library.

Upvotes: 3

Madhu
Madhu

Reputation: 11

Below is the simplest solution for viewing sqlite database in Android.

https://github.com/facebook/stetho

Steps to view SQLite DB

1) Add gradle dependency

compile 'com.facebook.stetho:stetho:1.5.0' 
implementation 'com.facebook.stetho:stetho:1.5.0' - for kotlin

2) In Application class

public class MyApplication extends Application {
  public void onCreate() {
   super.onCreate();
   Stetho.initializeWithDefaults(this);
  }
} 

3) Go to google chrome and type 'chrome://inspect/#devices' to see connected devices and can access SQlite DB.

Upvotes: 1

Rajesh N
Rajesh N

Reputation: 6673

This app is very useful, free and will remain free(Confirmed by the developer). Very easy to add, edit and delete.

It will work for rooted device and emulator. Also if you have DB file then you can browse it from SDCard of all devices including non-rooted.

enter image description here

Upvotes: 1

Shubham A.
Shubham A.

Reputation: 2534

Connect to Sqlite3 via ADB Shell

I haven't found any way to do that in Android Studio, but I access the db with a remote shell instead of pulling the file each time.

Find all info here: http://developer.android.com/tools/help/adb.html#sqlite

1- Go to your platform-tools folder in a command prompt

2- Enter the command adb devices to get the list of your devices

C:\Android\adt-bundle-windows-x86_64\sdk\platform-tools>adb devices
List of devices attached
emulator-xxxx   device

3- Connect a shell to your device:

C:\Android\adt-bundle-windows-x86_64\sdk\platform-tools>adb -s emulator-xxxx shell

4- Navigate to the folder containing your db file:

cd data/data/<your-package-name>/databases/

5- run sqlite3 to connect to your db:

sqlite3 <your-db-name>.db

6- run sqlite3 commands that you like eg:

Select * from table1 where ...;

Note: Find more commands to run below.

SQLite cheatsheet

There are a few steps to see the tables in an SQLite database:

  1. List the tables in your database:

    .tables
    
  2. List how the table looks:

    .schema tablename
    
  3. Print the entire table:

    SELECT * FROM tablename;
    
  4. List all of the available SQLite prompt commands:

    .help
    

Source : This SO answer..

Upvotes: 21

Related Questions