Skip to content

DatabaseHelper

nmc9 edited this page Apr 30, 2018 · 2 revisions

Database

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.

Example

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);
    }


}

Variables And Methods

REMOTE_DATABASE_LINK (Optional):

A String to store the url of your server;

AuthTable (Optional)

A String to store the name of the Authentication table for your app;

DATABASE_NAME:

The name of the local database

VERSION

An int representing the version number. Increment when making changes to database

Constructor

public Database(Context context) {
        super(context, REMOTE_DATABASE_LINK, DATABASE_NAME, null, VERSION);
}

all_tables()

Plug in all of the tables that you want in your project here. They will be created when the database is first called

getUpgradeHelper()

Create a new instance of your UpgradeHelper extended class and return it This is called whenever the database is upgraded