How to use ADB in Android Studio to view SQLite DB

I need to see my SQLite db, but I do not know how to do this. I went to http://www.sqlite.org/download.html and downloaded the command line shell for my OS, but when I run the program and type adb ... I get errors.

Note. I use Android Studio, so I assume that I do not need to install anything unnecessary, because I remember that Android Studio stated that it needed all the SDK tools.

+54
android adb
Aug 22 '13 at 1:24
source share
7 answers

The easiest way: connect to Sqlite3 through the ADB shell

I did not find any way to do this in Android Studio, but I am accessing the db with the shell removed, rather than pulling on the file each time.

Find all the information here: http://developer.android.com/tools/help/sqlite3.html

1- Go to your tools folder on the command line

2- Enter the adb devices command to get a 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 the shell to your device:

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

4a- You can bypass this step on the root device

 run-as <your-package-name> 

4b- Browse 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, for example:

 Select * from table1 where ...; 

Note. Find more commands to run below.

SQLite Encryption

There are several steps to see the tables in the SQLite database:

  • List of tables in your database:

     .tables 
  • List what the table looks like:

     .schema tablename 
  • Print the whole table:

     SELECT * FROM tablename; 
  • List all available SQLite command line commands:

     .help 
+91
Mar 23 '15 at 9:01
source share

The easiest way for me is to use the Android Device Monitor to get the database file and SQLite DataBase Browser to view the file when using Android Studio to program Android.

1) Launch and run the database application with Android emulator from Android Studio. (I installed some data in the database application for verification)

2) Launch Android Device Monitor. How to start?; Go to [your_folder] > sdk >tools . You can see monitor.bat in this folder. shift + right click inside the folder and select " Open command window here ". This action will launch the command line. type monitor and Android Device Monitor will be launched.

3) Select the emulator that you are currently using. Then go to data>data>[your_app_name]>databases

4) Click on the icon (located in the upper right corner) (hover over the icon and you will see โ€œpull the file from the deviceโ€) and save whatever you like

5) Launch the SQLite DataBase browser. Drag the file you just saved in this browser.

6) Go to the Browse Data tab and select your table to view.

enter image description hereenter image description here

+68
Jul 17 '14 at 16:35
source share

The problem you are facing is common and not well explained in the documentation. Regular devices do not include the sqlite3 binary database, so you get an error. Android Emeulator, OSX, Linux (if installed) and Windows (after installation) have a binary file, so you can open the database locally on your computer.

The workaround is to copy the database from your device to the local computer. This can be done using ADB, but requires a few steps.

Before you begin, you will need information:

  • SDK path (if it is not included in your OS environment)
  • your <package name> e.g. com.example.application
  • a <local path> to host your database, for example. ~/Desktop or %userprofile%\Desktop

Next, you will need to understand which terminal each command will be written to the first character in the examples below, is not entered, but allows you to find out which shell we are in:

  • > = OS command line command
  • $ = ADB Shell Command Hint
  • ! = ADB shell on admin command line
  • %

Next, enter the following commands from the terminal or command (do not enter the first character or text in () )

 > adb shell $ su ! cp /data/data/<package name>/Databases/<database name> /sdcard ! exit $ exit > adb pull /sdcard/<database name> <local path> > sqlite3 <local db path> % .dump % .exit (to exit sqldb) 

This is a really round-robin way to copy a database to a local machine and read the database locally. There are SO and other resources explaining how to install the sqlite3 binary on your device from the emulator, but within one time this process works.

If you need to access the database interactively, I would suggest running your application in an emulator (already having sqlite3) or installing sqlite on your devices / xbin path .

+13
Aug 22 '13 at 13:26
source share

What does he mention when entering adb?

 step1. >adb shell step2. >cd data/data step3. >ls -l|grep "your app package here" step4. >cd "your app package here" step5. >sqlite3 xx.db 
+8
Aug 22 '13 at 1:40
source share
  • Open terminal
  • cd <ANDROID_SDK_PATH> (for me on Windows cd C:\Users\Willi\AppData\Local\Android\sdk )
  • cd platform-tools
  • adb shell (this only works when starting only one emulator)
  • cd data/data
  • su (get superuser privileges)
  • cd <PACKAGE_NAME>/databases
  • sqlite3 <DB_NAME>
  • issue SQL statements ( important: terminate them with;; otherwise, the statement is not issued, and instead it breaks into a new line.)

Note. Use ls (Linux) or dir (Windows) if you need to specify the contents of a directory.

+5
Jan 08 '17 at 19:52
source share

Depending on the devices, you may not find the sqlite3 command in the adb shell. In this case, you may need the following:

adb shell

$ run-as package.name
$ cd. / databases /
$ ls -l # Start current permissions - r = 4, w = 2, x = 1
$ chmod 666./dbname.db
$ exit
$ exit
adb pull /data/data/package.name/databases/dbname.db ~ / Desktop /
adb push ~ / Desktop / dbname.db / data / data / package.name / databases / dbname.db
adb shell
$ run-as package.name
$ chmod 660. /databases/dbname.db #Restore source permissions
$ exit
$ exit

for reference, go to https://stackoverflow.com/a/166168/

There are times when you get a โ€œremote object not foundโ€ after performing the above procedure. Then change the resolution of the database folder to 755 in the adb shell.

$ chmod 755 databases /

But please note that it will only work in the Android version 21.

+4
Feb 23 '16 at 7:42 on
source share

You can use a very nice tool called Stetho by adding it to the build.gradle file:

 compile 'com.facebook.stetho:stetho:1.4.1' 

And initialized it inside the Application or Activity onCreate() method:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Stetho.initializeWithDefaults(this); setContentView(R.layout.activity_main); } 

Then you can view the db records in chrome at:

 chrome://inspect/#devices 

For more information you can read my post: How to easily view your db records

+4
Dec 25 '16 at 12:04 on
source share



All Articles