Android SQLite Experience

Before reading this post, check Room SQLite Framework for Android

I’ve had a lot of trouble to connect to my Android Studio emulator SQLite database to run queries. Stetho didn’t show my databases and amitshekhar just plain didn’t work for me.

The solution was to externally use ADB to connect to a rooted, non-google-play emulator, run a set of queries there and directly work with SQLite through Command Line Interface.

Troubles to connect to SQLite instance

ADB not found in system

ADB is installed with Android Studio. For windows, add the following link to your system environment variables.

C:\Users\{your_user}\AppData\Local\Android\Sdk\platform-tools

Log-out and log-in for it to take place. Then you should be able to open your CMD and run adb queries there.

adbd cannot run as root in production builds

This happens when you try to get sudo permissions in a Google Play emulator image. Just get into Android Studio AVD manager, create a new emulator and use an image which doesn’t have the Google Play icon.

ADB remount doesn’t work

One of the proposed solutions was to run this set of queries.

adb root
// this command failed
adb remount
sqlite3 data/data/your/app/path/database/your_database

which failed spectacularly.

avb_ops_user.cpp:217: ERROR: Error writing data.
avb_user_verity.c:205: ERROR: Error writing to partition 'vbmeta'
Skipping /system for remount
...
Operation not permitted
Can not mount overlayfs for partitions: No such file or directory
failed to remount partition dev:/dev/block/dm-3 mnt:/vendor: Read-only file system

This went nowhere. See solution below.

Connect to the emulator

Open CMD and run this set of queries to open a database connection.

adb shell
sqlite3 data/data/es.codes.mario.weighttracker/databases/entry_database

You should have an open connection now.
If this doesn’t work, try to run the following commands.

adb shell
su
mount -o rw,remount /system
sqlite3 data/data/es.codes.mario.weighttracker/databases/entry_database

SQLite Queries

I’m used to work with MySQL. This are queries which change for SQLite and are not guaranteed to be standard SQL.

.tables # list all tables
.schema tablename # describes the table