Is it possible to check if a db is created in other app? I have to have a common db for two app. If one app is already created the db then the second one should use the first apps' db (I mean should not create new one). For this,Is there a way to check whether that particular common db is created or not? Please help me on this.
5 Answers
This is not as easy as you may think. Even If you would definetly know, that your other app has created the db already, there is still difficulty in querying it. As any Db belongs to the app which created it, only this app usually gets access to it. Good news is that there is is a mechanism for that, called ContentProvider. With it you are able to share the db-infos between apps, but it takes some effort to implement this.

- 15,401
- 15
- 83
- 144
-
Thank you Rafael for your input.So you mean this is possible with ContentProvider? – Jey Jan 16 '13 at 11:19
-
of course. You can query for a special ContentProvider you define. If none is present, that means that the other app has created one, and you should create your DB yourself, providing a ContentProvider for the other app – Rafael T Jan 16 '13 at 12:23
The database is stored under /data/data/your.applications.package/databases
. Normally this location could only be access by the user the Android OS created for the app. No other user is able to access this location unless the device is rooted. Then any user can access any location on the phone and manipulate the data.
so you can check the whether the db is present in this location or not
you can directly check this Android Access Another App's Database

- 1
- 1

- 20,897
- 15
- 57
- 78
The best thing for you is to use the sdcard (or some other user accessible area, without root) Look at this thread
I know it's unsecure, but it's the best thing for this kind of problem. One application (the first one installed) creates the db on sdcard. The other one, just check's if the file (db) allready exists and reads from the existing db.

- 1
- 1

- 171
- 2
- 6
You should use SQLiteOpenHelper to control versioning of your database as described in database tutorial.
Unfortunately SQLiteOpenHelper only works in local app-directory but you can tell it to use a different directory on sd-card as described in sqliteopenhelper-with-fully-qualified-db-path-name
Try the following code
DB_PATH = "/data/data/THE_PACKAGE_NAME/databases/";
DB_NAME="YOUR_DB_NAME";
File dbFile = new File(DB_PATH + DB_NAME);
if(dbFile.exists()){
/* YOUR CODE HERE*/
}
Do the above check in both of your application with the PACKAGE NAME before creating the db.
:)

- 1,884
- 14
- 30