Debugging SQLite Database on Android Devices

Debugging SQLite Database on Android Devices

30 November 2024 Stephan Petzl Leave a comment Tech-Help

Working with databases on Android devices can be challenging, especially when trying to access and debug SQLite databases directly on the device. This guide provides a step-by-step approach to accessing your application’s database without rooting your device, leveraging some of the most effective methods available.

Accessing the Database with ADB Commands

One of the most reliable ways to access your application’s SQLite database is by using the run-as command within the Android Debug Bridge (ADB). This approach requires that your application is built as debuggable. Here’s how you can achieve this:

Copying Files to External Storage

  1. Launch a debug build of your application from Android Studio.
  2. Connect to the device using ADB shell and execute the following command:
  3. adb shell 'run-as com.your.package cat databases/db-file.db > /sdcard/db-file.db'
  4. This command copies the database file to the root of your SD card or external storage, making it accessible for further actions.

Directly Copying Files to Your Computer

Alternatively, you can directly transfer the database file to your computer:

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

Note: If you’re using a Windows machine, you may need to use the adb exec-out command to avoid issues with line-ending conversions:

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

Utilizing Android Studio’s Database Inspector

For users of Android Studio 4.1 and later, the Database Inspector is a powerful tool that allows you to view and modify your app’s databases in real-time without the need for manual file transfers.

  1. Navigate to View > Tool Windows > Database Inspector to open it.
  2. Ensure your app runs on a device with API level 26 or higher to utilize this feature effectively.

This tool provides a visual interface for querying and modifying your databases directly from Android Studio.

Exploring Third-Party Libraries

Libraries like Android Debug Database offer an alternative by enabling database inspection through a web interface. By adding a simple dependency to your project, you can access your database tables using a web browser.

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

Enhancing Your Testing Strategy with Repeato

While debugging databases is crucial, effective testing is equally important. Our product, Repeato, offers a no-code test automation solution for iOS and Android apps. With Repeato, you can create, run, and maintain automated tests seamlessly.

Repeato integrates ADB, allowing you to execute ADB commands via script steps, ensuring precise control over your testing processes. This capability complements database debugging by providing a comprehensive testing environment that enhances your app’s reliability.

For more information on setting up virtual test devices or running test batches, visit our documentation page.

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