问题
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
回答1:
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
回答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
);
回答3:
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);
来源:https://stackoverflow.com/questions/16142844/open-sqlite-database-of-another-app-on-android-with-root-access