Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open sqlite-database of another app on Android with root-access

I want to open the database of another app. I know I must have root-access, but it seems that root access is "only" for shell commands.

I want to make a lot of selects and some inserts into the database. Is it possible to open the db as root, and work with the db-handle in the "normal" app?

Thanks in advance

Biber

like image 498
Biber Avatar asked Mar 24 '23 05:03

Biber


2 Answers

thanks for all answers! I think the only way is to make something like this:

Process p = Runtime.getRuntime().exec("su sqlite3 -csv test.db \"select * from test\";");

Then, I must parse the OutputStream with a cvs parser,.... I hoped I can do it in a simpler way, but I see no solution.

Maybe I can create a hard link to a file in a directory of my app, but it is very dangerous, because in that way there are two ".journal" files for one db.

Thanks for help

Biber

like image 79
Biber Avatar answered Apr 06 '23 02:04

Biber


You still can access the database if you have the root access through shell commands :

Example :

mycomp$ adb shell
$ su
# cd com.android.providers.media
# ls
cache
databases
lib
shared_prefs
# cd databases
# ls
external.db
external.db-shm
external.db-wal
internal.db
internal.db-shm
internal.db-wal
# sqlite3 external.db
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select count(*) from images;
10
sqlite> 

The tool used is sqlite3 which is a client command to an sqlite database. The database files are usually located in /data/data/com.someapp/databases/.

Edit : Wait... I was re reading your question. Do you mean you want to access a database of another app from your own app?

Edit : If you want to access another database, the other database has to be a content provider. The best example of that is the media library (the image table above is the table that content the picture in your device). Code sample :

 // which image properties are we querying
 String[] projection = new String[] { BaseColumns._ID, ImageColumns.BUCKET_DISPLAY_NAME, ImageColumns.DATE_TAKEN, MediaColumns.TITLE, MediaColumns.DATA };

 // Get the base URI for the image table in the Contacts content provider.
 Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

 // Make the query.
 Cursor cur = context.managedQuery(images, projection, // Which columns to return
            "", // Which rows to return (all rows)
            null,//selection, // Selection arguments (none)
            ImageColumns.DATE_TAKEN + " DESC"// Ordering
            );
like image 37
Gomoku7 Avatar answered Apr 06 '23 03:04

Gomoku7