Reputation: 497
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
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
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
Upvotes: 2
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