Share Sqlite Database Between 2 Android Apps

How to Share Sqlite Between Two Application?

The short answer: Yes.

The long answer: You will have to "register" your application to share its contents with your second application. That being said, the down side is, you will be sharing your contents with every other application too. Not that I see it being a problem since other developers will not necessarily know how to gain access to your app's data using the Content Providers.

You can follow this link to read up about creating and access content providers.

Oh. Almost forgot. This is how you share data between applications. I have personally never heard of anyone having shared databases. DB's are always private to their own application and never exposed to others including the users. See this link for more about Databases in Android

using the same sqlite database for multiple android devices

Will this database be the same database on all phones who downloads the app,

The Answer is NO Because SQLite datbase stored locally in the device

which means if person 1 on a phone writes something to the database, person 2 with another phone will immediately see the update?

Not Possible with SQLite

I'm interested in creating a database for my app that works as one database for all the phones, so that person 1 and person 2 will use the exact same database.

You can use Firebase Realtime Database

  • Store and sync data with our NoSQL cloud database. Data is synced across all clients in realtime, and remains available when your app goes offline.
  • The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. When you build cross-platform apps with our iOS, Android, and JavaScript SDKs, all of your clients share one Realtime Database instance and automatically receive updates with the newest data

Sharing same SQLite DB for two android apps

I would recommend what you've asked about - which is using a remote database.

Check out this: how to use free cloud database with android app?

Look into a hosted database in the cloud. Quite a few of them will let you get started for free. I know IrisCouch lets you get going for free and it's very easy. http://www.iriscouch.com/ The nice thing about CouchDB is you can do all your work via HTTP - which isn't too hard from an Android device.

SQLite Database in Android library, Will it be shared among apps?

SQL databases of each application is stored in their private folder on the storage. So the path would be as follow.

/data/data/[app_package_name]/databases/[database_name]

Applications cannot share data with each other using SQLite databases.

If we want data to get shared among other application we can use Content Providers.

http://developer.android.com/reference/android/content/ContentProvider.html

Share SQLite database from Android app, without intermediate copy

I solved this on my own. I'm documenting it here, per Neil's request.

This is where I launch the export/backup from my activity:

public class MyActivity {

private void exportUserContent() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("application/octet-stream");

Uri uri = new FileProvider().getDatabaseURI(this);

intent.putExtra(Intent.EXTRA_STREAM, uri);

startActivity(Intent.createChooser(intent, "Backup via:"));
}

}

The FileProvider:

public class FileProvider extends android.support.v4.content.FileProvider {

public Uri getDatabaseURI(Context c) {
// https://developer.android.com/reference/android/support/v4/content/FileProvider.html
// old approach that worked until 2020-ish
// File data = Environment.getDataDirectory();
// String dbName = "UserContent.db";
// String currentDBPath = "//data//com.url.myapp//databases//" + dbName;

// File exportFile = new File(data, currentDBPath);
File exportFile = c.getDatabasePath(dbName); // new approach

return getFileUri(c, exportFile);
}

public Uri getFileUri(Context c, File f){
return getUriForFile(c, "com.url.myapp.fileprovider", f);
}

}

Inside AndroidManifest.xml:

<manifest ...>
<application ...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.url.myapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>

Inside \app\src\main\res\xml\filepaths.xml
(I think the first entry is the relevant one, but I'll include the whole file for completeness):

<paths>
<files-path
path="../databases/"
name="mydatabases"/>

<files-path
path=""
name="migrations"/>

<external-path
path=""
name="external"/>
</paths>


Related Topics



Leave a reply



Submit