-
Notifications
You must be signed in to change notification settings - Fork 1
DatabaseHelper
nmc9 edited this page Apr 30, 2018
·
2 revisions
The Database class you create should extend the DatabaseHelper class. This class has the functions to create and query the database defined in your subclass.
public final class Database extends DatabaseHelper {
public static final String REMOTE_DATABASE_LINK = "{yourserver.com}";
//Optional Authentication Table
public static final String AuthTable = UsersTable.TABLE_NAME;
//Name of local Database
public static final String DATABASE_NAME = "local_database";
//Version Number
private static final int VERSION = 3;
public Database(Context context) {
super(context, REMOTE_DATABASE_LINK, DATABASE_NAME, null, VERSION);
}
//List of the tables in your project
@Override
public ArrayList<TableHelper> all_tables() {
ArrayList<TableHelper> TABLES = new ArrayList<>();
TABLES.add(new UsersTable(this));
return TABLES;
}
// Create a new instance of UpgradeHelper Class
@Override
protected UpgradeHelper getUpgradeHelper(SQLiteDatabase db, int oldVersion, int newVersion) {
return new Upgrade( db, oldVersion, newVersion);
}
}
A String to store the url of your server;
A String to store the name of the Authentication table for your app;
The name of the local database
An int representing the version number. Increment when making changes to database
public Database(Context context) {
super(context, REMOTE_DATABASE_LINK, DATABASE_NAME, null, VERSION);
}
Plug in all of the tables that you want in your project here. They will be created when the database is first called
Create a new instance of your UpgradeHelper extended class and return it This is called whenever the database is upgraded