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

Bongo
  • 2,933
  • 5
  • 36
  • 67
Biber
  • 709
  • 6
  • 19

3 Answers3

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

Biber
  • 709
  • 6
  • 19
  • How do you read the output of this code? Thank you for your reply – Mohammad Mar 21 '17 at 06:27
  • 1
    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. – Biber Mar 21 '17 at 09:22
1

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
            );
Gomoku7
  • 1,362
  • 13
  • 31
  • Yes, I want access it from my app. I can execute the sqlite3 client with su, but then, I have no real SQL-Result,.. – Biber Apr 22 '13 at 09:17
  • With su under a shell you should be able to execute any query without limitation. I edit my answer with content providers. – Gomoku7 Apr 22 '13 at 13:00
  • 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? – Biber Apr 22 '13 at 13:37
  • Then you'll have to give your app the root access (like here http://muzikant-android.blogspot.fr/2011/02/how-to-get-root-access-and-execute.html) copy the target database on your own database app folder and work as if the database was local to your app. – Gomoku7 Apr 22 '13 at 13:46
  • 1
    There's an interesting discussion here : http://stackoverflow.com/questions/7053809/share-sqlite-database-between-2-android-apps – Gomoku7 Apr 22 '13 at 13:49
  • Is it possibe to do this without copying the db? I want to read the other's app data, and show it in another way. It is recommend that the other app is working on the db during I read from it,.... – Biber Apr 22 '13 at 13:53
  • Maybe it worked with a hardlink, which I create with root-privileges. I think reading is ok, but if I start to write in it, the db will be crash. – Biber Apr 22 '13 at 13:55
-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);
Acuna
  • 1,741
  • 17
  • 20