Databases 5 min read

Using SQLite Databases and Advanced ADB Commands on Android

This guide explains how to access Android SQLite databases via adb shell sqlite3, create and run complex shell scripts, and manage app permissions and users through ADB commands, providing practical examples for debugging and automation.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using SQLite Databases and Advanced ADB Commands on Android

Many Android applications rely on SQLite databases for data storage. By using the adb shell sqlite3 command, you can directly access and manipulate these databases, which is useful for debugging and verifying data storage logic.

Entering the SQLite command‑line tool:

adb shell sqlite3 /data/data/
<package_name>
/databases/
<database_name>

Replace <package_name> with the target app’s package name and <database_name> with the specific database file you wish to inspect.

Example: to view the database mydb.db of the app com.example.myapp , run:

adb shell sqlite3 /data/data/com.example.myapp/databases/mydb.db

Once inside the SQLite environment, you can execute standard SQL commands. For instance:

.tables
SELECT * FROM users;
adb exec -out run-as com.example.myapp sqlite3 /databases/mydb.db .dump > mydb_dump.sql

Executing Complex Shell Scripts

When a single command is insufficient, you can write a shell script to automate a series of operations on the device. Create a file script.sh on your computer with the following content:

#!/system/bin/sh
 echo "开始清理临时文件..."
 rm -rf /sdcard/tmp/*
 echo "清理完成"

Push the script to the device, grant execution permission, and run it:

adb push script.sh /data/local/tmp/
adb shell chmod +x /data/local/tmp/script.sh
adb shell /data/local/tmp/script.sh

The commands will be executed in the order defined in the script.

Permission Management and User Operations

Proper permission handling is crucial during development. ADB provides commands to list, grant, and revoke permissions.

adb shell pm list permissions -g -d

This lists all declared permissions and their current grant status.

adb shell pm grant android.permission.WRITE_EXTERNAL_STORAGE

Grants the specified permission.

adb shell pm revoke android.permission.WRITE_EXTERNAL_STORAGE

Revokes the specified permission.

For multi‑user devices, you can list users:

adb shell pm list users

Switching users via ADB is more complex and often requires UI automation, but the command above helps you identify existing user accounts.

Conclusion

By following this tutorial you should now be able to query SQLite databases, write and execute advanced shell scripts, and manage permissions and user accounts using ADB, thereby improving your efficiency in Android development and debugging. Upcoming articles will cover the basics of Monkey testing.

androidDatabaseSQLiteADBPermissionsShell Script
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.