How to access the database when developing on Android phone?

20,598

Solution 1

To answer the first part of your question, check out this answer. Basically, your phone needs to have root access, and you need to run adb in root mode (using "adb root").

As for the second part, I use SQLite Database Browser to view my SQLite dbs (though that's only when the db is on my computer; don't know of any on-device browsers). I don't know of any way to get a human view of content resolvers.

Solution 2

Check my answer from the following thread: Why do I get access denied to data folder when using adb?

Starting from API level 8 (Android 2.2), if you build the application as debuggable, you can use the shell run-as command to run a command or executable as a specific user/application or just switch to the UID of your application so you can access its data directory.

So basically you will need to make a debug build (which is made automatically whenever you launch the application from the Android Studio unless you request the release build) and run the following commands:

run-as com.mycompck
cd /data/data/com.mycompck/databases
ls
sqlite3 ./yourdatabase.db

However note that sqlite3 binary is not present by default on many phones. Thus you will perhaps need to download it somewhere (e.g. from SuperOneClick archives at http://shortfuse.org/), save on the SD card and make it executable (which is a little bit tricky though), for example:

run-as com.mycompck
cd /data/data/com.mycompck/
cat /sdcard/sqlite3 >./sqlite3
chmod 744 ./sqlite3
./sqlite3 ./databases/yourdatabase.db
Share:
20,598
Pentium10
Author by

Pentium10

Backend engineer, team leader, Google Developer Expert in Cloud, scalability, APIs, BigQuery, mentor, consultant. To contact: message me under my username at gm ail https://kodokmarton.com

Updated on July 11, 2020

Comments

  • Pentium10
    Pentium10 almost 4 years

    I am having trouble accessing the database while I am developing on the phone. Whenever I execute

    cd /data/data/com.mycompck/databases

    and then run ls I get: "opendir failed, Permission denied"

    Or whenever I type in sqlite3 I get: "sqlite3: permission denied"

    What am I doing wrong?

    Are there some applications that can help me getting a human view of content resolvers values and/or SQLite databases?

  • Pentium10
    Pentium10 about 14 years
    Why do I need root access, I want to access my applications data, as I am the developer I would love to get access to it.
  • Pentium10
    Pentium10 about 14 years
    adbd cannot run as root in production builds
  • Pentium10
    Pentium10 about 14 years
    Unfortunately I can't debug from Emulator the database as the emulator doesn't have the apps I am using (Agenda, Bluetooth, Wifi).
  • Cristian
    Cristian about 14 years
    But you can install those apps to the emulator: adb install appname.apk Anyway, I highly recommend to root your phone :) google.com.co/search?q=root+android
  • CommonsWare
    CommonsWare about 14 years
    Add an export or backup function to your app that copies the database to the SD card.
  • Dan Lew
    Dan Lew about 14 years
    @Pentium10: Because that's the way it works, access to the data directory is locked for non-root access. I don't make the rules. :P CommonsWare is correct though, that's a decent workaround.
  • Idolon
    Idolon over 12 years
    @Pentium10 Things has changed a little since 2010 :) Check my answer below.
  • styfle
    styfle over 12 years
    So you're copying sqlite3 to the app's directory and then changing permissions to executable. I'm guessing this will mean a copy of sqlite3 will live in that directory until the app is deleted off the phone. So my question is: does reinstalling the app delete the executable (for example, clicking RUN in Eclipse)?
  • Idolon
    Idolon over 12 years
    @styfle Your application database and shared preferences aren't deleted when you click "Run" in Eclipse, are they? So do sqlite3 executable. Only adb uninstall (without -k option) will delete it (or manual application uninstall through the device Application Manager).
  • IronBlossom
    IronBlossom about 12 years
    @CommonsWare Thanks.. very simple thought...Keep making people a Busy coder.
  • Rupert
    Rupert over 10 years
    Do I need to do anything in Idea13CE (community edition) to ensure the it's making a debug build?
  • S.L. Barth
    S.L. Barth almost 7 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
  • Feddex
    Feddex almost 7 years
    good point. but still, its a great way to manage your Mobile Databases