Debug sqlite database on device

I am currently working on a WiFi app for Android. I am having trouble trying to access the database on the device. Debugging in the emulator does not work for me, because the emulator does not have WiFi support. I tried to pull the database file from the device using

adb pull data/data/package-name/databases/database-name 

But I get the error "Permission denied.". In this Android answer : Where are the database files stored? Commonsware suggested pulling out a database file by running it in debug mode. But that doesn't work either. Any help on how to debug the database without rooting the device would be greatly appreciated.

+82
android sqlite adb
Aug 03 2018-11-11T00:
source share
13 answers

I repeat from another answer :

Starting from API level 8 (Android 2.2), if you are creating the application as a debugging application, you can use the shell run-as command to run the command or executable file as a specific user / application or simply switch to the UID your application so that you can access to his data directory.

So, if you want to pull your application database from the device, you must start the debug build of the application, connect to the adb shell and run the following command:

 run-as com.yourpackage sh -c "cat ~/databases/db-file" > /sdcard/db-file.sqlite 

This will copy your db-file to the root of your SD card / external storage. Now you can easily get it from there using the adb pull file manager or whatever you like. Please note that with this approach, it is not necessary for your application to have WRITE_EXTERNAL_STORAGE permission, since copying is performed by the shell user, who can always write to external memory.

On Linux / Mac systems, it is possible to copy the database directly to your computer using the following command, which can be used without entering the adb shell:

 adb shell 'run-as com.yourpackage sh -c "cat ~/databases/db-file"' > db-file.sqlite 

This, however, will not work correctly on Windows due to CR / LF character conversion. Use the old method there.

+130
Dec 08 '11 at 16:45
source share

I use this shell script on my MAC address, which copies the database directly to my home folder. Simple one-click solution, just change the package name (com.example.app) and the database name (database.sqlite)

Simple script

 #!/bin/bash adb -d shell 'run-as com.example.app cat /data/data/com.example.app/databases/database.sqlite > /sdcard/database.sqlite' adb pull /sdcard/database.sqlite ~/ 

Script that takes arguments [package_name] [database]

 #!/bin/bash REQUIRED_ARGS=2 ADB_PATH=/Users/Tadas/Library/sdk/platform-tools/adb PULL_DIR="~/" if [ $# -ne $REQUIRED_ARGS ] then echo "" echo "Usage:" echo "android_db_move.sh [package_name] [db_name]" echo "eg. android_db_move.sh lt.appcamp.impuls impuls.db" echo "" exit 1 fi; echo"" cmd1="$ADB_PATH -d shell 'run-as $1 cat /data/data/$1/databases/$2 > /sdcard/$2' " cmd2="$ADB_PATH pull /sdcard/$2 $PULL_DIR" echo $cmd1 eval $cmd1 if [ $? -eq 0 ] then echo ".........OK" fi; echo $cmd2 eval $cmd2 if [ $? -eq 0 ] then echo ".........OK" fi; exit 0 
+21
Oct. 16
source share

The best way to view and manage your Android application database is to use this library https://github.com/sanathp/DatabaseManager_For_Android

Using this library, you can manage your SQLite application database directly from your application. You can view tables in your application database, update, delete, insert rows in your tables. Everything from your application.

Its the only java activity file, just add the java file to the source folder. After development is complete, delete the java file from your src folder.

It helped me a lot. Hope this helps too.

You can watch the 1 minute demo here: http://youtu.be/P5vpaGoBlBY

+12
Jul 21 '14 at 1:27
source share

In my application, I export the database to an SD card. After the database is located on the SD card, it can be obtained by connecting the device to the computer.

Take a look at this post: Backing up a database on an SDCard on Android

+5
Aug 03 '11 at 15:38
source share

If you get

 The system cannot find the path specified. 

to try

 adb -d shell "run-as com.yourpackage cat /data/data/com.yourpackage/databases/dbname.sqlite > /sdcard/dbname.sqlite" 

Pay attention to the double quote!

+5
Nov 22 '12 at 16:29
source share

I just did:

 $ adb shell shell@android:/ $ run-as myapp.package.name sh shell@android:/data/data/myapp.package.name $ 

Then I can debug the sqlite database or whatever I want to do from the shell with the correct permissions.

+3
Jun 30 '13 at 13:46
source share

There is a way if apk is debugged to use a program called run-as from the (non-root) adb shell to copy a closed application file.

+2
Aug 03 '11 at 18:18
source share

Here are step-by-step instructions - mostly taken from a combination of other answers. This works with devices that are not unlocked.

  • Connect the device and run the application in debug mode.

  • Copy the database file from the folder of your application to your SD card: do:

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

  • Pull the database files to your computer: do:

    ./adb pull / sdcard / execute: ./ adb

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

  • Open Firefox SQLLite Manager and open the database file from step 3 above.

  • Enjoy it!

+2
Jan 16 '14 at 1:11
source share

You need to run adb as root or use it on the root phone.

To run adb as root, use adb root

See also: Why am I accessing the data folder when using adb?

+1
Aug 03 2018-11-11T00:
source share

None of the run-as -and-cat-to-sdcard solutions worked on Android 4.4.2. I'm not sure, but I suspect that this may be because the run-as tool does not correctly handle the new sdcard_r and sdcard_rw .

First I had to copy the database file in /files to my personal application store:

 shell@hammerhead:/ $ run-as com.example.myapp shell@hammerhead:/data/data/com.example.myapp $ cp databases/mydb files/mydb 

Then I copied to /sdcard/Android/data/com.example.myapp/files in Javaland (this requires permission WRITE_EXTERNAL_STORAGE ):

 public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { ... if (isExternalStorageWritable()) { final FileInputStream input; try { input = openFileInput("mydb"); File output = new File(getExternalFilesDir(null), "mydb"); copy(input, output); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } public void copy(FileInputStream in, File dst) throws IOException { OutputStream out = new FileOutputStream(dst); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } public boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); return Environment.MEDIA_MOUNTED.equals(state); } } 

Finally, I copied the file to my laptop:

 $ adb pull /sdcard/Android/data/com.example.myapp/files/mydb 
+1
May 11 '14 at 15:36
source share

My device did not have sdcard

so the first solution did not work for me.

If you have the same problem, try like this:

  adb shell "run-as package chmod 777 /data/data/package/databases/yourdb.sqlite"; adb pull /data/data/package/databases/yourdb.sqlite 
+1
Jun 16 '15 at 7:13
source share

Try this app: SQLiteWeb ( https://play.google.com/store/apps/details?id=br.com.cm.sqliteweb ). It provides remote access to your database without pulling it out.

In the paid version, it has root access for a private database (/ data / data / package-name ...) or implements a Content Provider for connecting using SQLiteWeb (instructions inside the application)

But if you want to stay with the free version, you can create your database in external storage:

 database = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory() + "/" + DATABASE_NAME, null, DATABASE_VERSION); 
0
Jan 14 '16 at 13:00
source share

On OSX, using @Tadas's answer using automator and sqllitebrowser ( https://github.com/sqlitebrowser/sqlitebrowser ):

  • open Automator and create a new workflow.

  • add action "Run Shell Script".

  • Insert this:

    source ~ / .bash_profile adb shell 'run-as cat / data / data / your_app_uid / databases / db.name> /tmp/db.name' adb pull / tmp / db.name ~ / open -a sqlitebrowser ~ / db. name

  • click to update the database on sqlitebrowser.

0
Apr 26 '16 at 11:19
source share



All Articles