Frederic
Frederic

Reputation: 497

using the same sqlite database for multiple android devices

I have created a little demo app in which I've been trying to implement an SQLite Database. Everything is working, however being new to mobile development I'm not sure how the database works.

    public class DBOpenHelper extends SQLiteOpenHelper{

    //Constants for db name and version
    private static final String DATABASE_NAME = "notes.db";
    private static final int DATABASE_VERSION = 1;

    //Constants for identifying table and columns
    public static final String TABLE_NOTES = "notes";
    public static final String NOTE_ID = "_id";
    public static final String NOTE_TEXT = "noteText";
    public static final String NOTE_CREATED = "noteCreated";

    public static final String[] ALL_COLUMNS =
            {NOTE_ID, NOTE_TEXT, NOTE_CREATED};

    //SQL to create table
    private static final String TABLE_CREATE =
            "CREATE TABLE " + TABLE_NOTES + " (" +
                    NOTE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    NOTE_TEXT + " TEXT, " +
                    NOTE_CREATED + " TEXT default CURRENT_TIMESTAMP" +
                    ")";

    public DBOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTES);
        onCreate(db);
    }
}

Will this database be the same database on all phones who downloads the app, which means if person 1 on a phone writes something to the database, person 2 with another phone will immediately see the update?

or is it the other way around: person 1 has one database and person 2 will have another database for himself?

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.

Upvotes: 1

Views: 3606

Answers (3)

Amitabh Sarkar
Amitabh Sarkar

Reputation: 1311

These databases are created locally in the device, which does not have any connection with the outside world, every device which runs the app with creating there own local database, you should use Firebase Real-time Database make a single database which syncs whenever someone changes and it will reflect on all other devices.

Heare is a good example to start with. See the attached video before starting.

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69689

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

Upvotes: 2

john-salib
john-salib

Reputation: 793

SQLite is a local DB, so every person will have his own unique data, nothing is going to be shared

If you are looking for something to be synchronized between multiple devices, you should consider having a backend server to receive updates from one device and send this update to other devices

You could also use Firebase real-time database

Upvotes: 3

Related Questions