4

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

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

2 Comments

How do you read the output of this code? Thank you for your reply
You must parse the outputstream of the process "p". The commandline parameter "-csv" means, that the output is in csv-format, so you must parse it as csv.
2

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
            );

7 Comments

Yes, I want access it from my app. I can execute the sqlite3 client with su, but then, I have no real SQL-Result,..
With su under a shell you should be able to execute any query without limitation. I edit my answer with content providers.
I don't have access to the other db/app, so I can't set a content provider.I want full access to the db with the java class SQLiteDatabase, not only with "su sqlite3 SQLCOMMAND" or something else, is it possible?
Then you'll have to give your app the root access (like here muzikant-android.blogspot.fr/2011/02/…) copy the target database on your own database app folder and work as if the database was local to your app.
There's an interesting discussion here : stackoverflow.com/questions/7053809/…
|
-1

You don't need the root access and shell, if these two apps have an sharedUsersId tag with same value in Manifest:

<manifest
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:sharedUserId="app.you.want.to.share"
>

and in app, where you want to access the app.you.want.to.share app you must create a Context:

Context sharedContext = createPackageContext ("app.you.want.to.share", Context.CONTEXT_INCLUDE_CODE);

then you can use it in DB adapter, etc:

class DBHelper extends SQLiteOpenHelper {

    DBHelper (Context context) {
      super (context, "db", null, 1);
    }

}

DBHelper dbHelper = new DBHelper (sharedContext);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.