Accessing SQLite Database on Android Devices Without Root

Accessing SQLite Database on Android Devices Without Root

22 April 2024 Stephan Petzl Leave a comment Tech-Help

Developers often face the challenge of accessing and debugging SQLite databases on Android devices, especially when the emulator is not an option, such as when testing WiFi-related applications. This article provides a guide on how to access your app’s SQLite database directly from an Android device without needing root access.

Using run-as Command

The run-as command is available from API level 8 (Android 2.2) and allows developers to execute commands as the app user, enabling access to the app’s data directory. Here’s how you can leverage this for database access:

Copying the Database to External Storage

To copy the database file to external storage, use the following command after connecting to the device via ADB:

adb shell 'run-as com.your.package cat databases/db-file.db' > /sdcard/db-file.db

This command does not require the WRITE_EXTERNAL_STORAGE permission as the shell user has write access to external storage.

Copying the Database Directly to Your Computer

For copying the database file directly to your computer, execute:

adb shell 'run-as com.your.package cat databases/db-file.db' > db-file.db

Windows users might face issues with CR/LF symbols conversion. In such cases, use the adb exec-out command:

adb exec-out 'run-as com.your.package cat databases/db-file.db' > db-file.db

Writing Files Back to the Device

If you have modified the database file on your computer and wish to write the changes back to the device, use:

adb exec-in 'run-as com.your.package tee databases/db-file.db' < db-file.db

Database Inspector in Android Studio

For those using Android Studio 4.1 or later, the Database Inspector is a powerful tool that allows you to inspect, query, and modify your database in real-time. You can access it via View > Tool Windows > Database Inspector in the menu bar.

Third-Party Libraries

There are also third-party libraries like DatabaseManager_For_Android and tools like Android Debug Database that can help manage and inspect databases within the app itself or through a web interface.

Scripting Solutions

Some developers prefer to use shell scripts to automate the process of copying the database from the device. These scripts can be customized to fit the specific package name and database file.

Repeato – Streamlining Your Testing Workflow

While accessing and debugging databases is crucial, ensuring your app’s functionality through testing is just as important. Repeato is a no-code test automation tool for iOS and Android that can significantly enhance your testing process. It supports various app frameworks and allows for the execution of ADB commands, making it a versatile choice for developers working with databases.

Repeato’s AI-based approach and computer vision capabilities enable fast editing and running of automated tests, ensuring high efficiency and reliability in your testing workflow. Whether you’re dealing with SQLite databases or any other app component, Repeato can help maintain a robust testing environment.

Explore more about Repeato and how it complements your development and testing toolkit by visiting our comparison with Appium.

Like this article? there’s more where that came from!