SQLite Exercise- Contract and Database

github: T07.01-CreateAContract  and T07.02- Create The Database

IMG_20170918_162612

Step 1:

// TODO (1) Create an inner class named WaitlistEntry class that implements the BaseColumns interface
public static final class WaitlisEntry implements BaseColumns{
    // TODO (2) Inside create a static final members for the table name and each of the db columns
    public static final String TABLE_NAME= "waitlist";
    public static final String COLUMN_GUEST_NAME= "guestName";
    public static final String COLUMN_PARTY_SIZE = "partySize";
    public static final String COLUMN_TIMESTAMP = "timestamp";

}

Step 2:

// TODO (1) extend the SQLiteOpenHelper class
public class WaitlistDbHelper extends SQLiteOpenHelper {

    // TODO (2) Create a static final String called DATABASE_NAME and set it to "waitlist.db"
    public static final String DATABASE_NAME = "waitlist.db";
    // TODO (3) Create a static final int called DATABASE_VERSION and set it to 1
    public static final int DATABASE_VERSION = 1;

    // TODO (4) Create a Constructor that takes a context and calls the parent constructor
    public WaitlistDbHelper(Context context){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    // TODO (5) Override the onCreate method
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO (6) Inside, create an String query called SQL_CREATE_WAITLIST_TABLE that will create the table
        final String SQL_CREATE_WAITLIST_TABLE = "Create Table "+
                WaitlistContract.WaitlistEntry.TABLE_NAME +" ( "+
                WaitlistContract.WaitlistEntry._ID+" Integer Primary Key Autoincrement, "+
                WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME+" Text Not Null, "+
                WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE+" Integer Not Null, "+
                WaitlistContract.WaitlistEntry.COLUMN_TIMESTAMP+" Timestamp Default Current_Timestamp"+
                " ); ";

        // TODO (7) Execute the query by calling execSQL on sqLiteDatabase and pass the string query SQL_CREATE_WAITLIST_TABLE
        db.execSQL(SQL_CREATE_WAITLIST_TABLE);
    }

    // TODO (8) Override the onUpgrade method
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        // TODO (9) Inside, execute a drop table query, and then call onCreate to re-create it
        // For now simply drop the table and create a new one. This means if you change the
        // DATABASE_VERSION the table will be dropped.
        // In a production app, this method might be modified to ALTER the table
        // instead of dropping it, so that existing data is not deleted.
        db.execSQL("Drop Table if exists "+ WaitlistContract.WaitlistEntry.TABLE_NAME);
        onCreate(db);
    }
}

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s