Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

python - How to use a script to query android sqlite database

Android saved settings in a database file which is /data/data/com.android.providers.settings/databases/settings.db.

Android use sqlite3 as the database. We can use adb to manage the database file. I want to know if there is a way to run all these commands in a perl/python script to automate the whole query process?

$adb shell 
$sqlite3 /data/data/com.android.providers.settings/databases/settings.db 

The above command will open the settings database. Then you will enter into sqlite3 command line.

First check how many tables existed in the database. Here lists the result.

sqlite> .tables

android_metadata   bookmarks          gservices        
bluetooth_devices  favorites          system  

The settings (such as volume_alarm) I want to get are in "system" table, a .dump command will list all items in the table.

sqlite> .dump system
BEGIN TRANSACTION;
CREATE TABLE system (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT UNIQUE ON CONFLICT REPLACE,value TEXT);
INSERT INTO "system" VALUES(3,’volume_system’,’5′);
INSERT INTO "system" VALUES(4,’volume_voice’,’4′);
INSERT INTO "system" VALUES(5,’volume_alarm’,’6′);
.....
$ select value from system where name ='volume_alarm';
select value from system where name ='volume_alarm'
6
$.quit;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To query a specific value:

adb shell sqlite3 /data/data/com.android.providers.settings/databases/settings.db "select value from 'system' where name = 'volume_alarm';"

Or to pull all records from a table:

adb shell sqlite3 /data/data/com.android.providers.settings/databases/settings.db "select name, value from 'system';"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...