· 8 years ago · Jan 24, 2018, 07:40 AM
1package com.example.android.waitlist.data;
2
3
4import android.content.Context;
5import android.database.sqlite.SQLiteDatabase;
6import android.database.sqlite.SQLiteOpenHelper;
7
8import com.example.android.waitlist.data.WaitlistContract.*;
9
10// COMPLETED (1) extend the SQLiteOpenHelper class
11public class WaitlistDbHelper extends SQLiteOpenHelper {
12
13 // COMPLETED (2) Create a static final String called DATABASE_NAME and set it to "waitlist.db"
14 // The database name
15 private static final String DATABASE_NAME = "waitlist.db";
16
17 // COMPLETED (3) Create a static final int called DATABASE_VERSION and set it to 1
18 // If you change the database schema, you must increment the database version
19 private static final int DATABASE_VERSION = 1;
20
21 // COMPLETED (4) Create a Constructor that takes a context and calls the parent constructor
22 // Constructor
23 WaitlistDbHelper(Context context) {
24 super(context, DATABASE_NAME, null, DATABASE_VERSION);
25 }
26
27 // COMPLETED (5) Override the onCreate method
28 @Override
29 public void onCreate(SQLiteDatabase sqLiteDatabase) {
30
31 // COMPLETED (6) Inside, create an String query called SQL_CREATE_WAITLIST_TABLE that will create the table
32 // Create a table to hold waitlist data
33 final String SQL_CREATE_WAITLIST_TABLE = "CREATE TABLE " + WaitlistEntry.TABLE_NAME + " (" +
34 WaitlistEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
35 WaitlistEntry.COLUMN_GUEST_NAME + " TEXT NOT NULL, " +
36 WaitlistEntry.COLUMN_PARTY_SIZE + " INTEGER NOT NULL, " +
37 WaitlistEntry.COLUMN_TIMESTAMP + " TIMESTAMP DEFAULT CURRENT_TIMESTAMP" +
38 "); ";
39
40 // COMPLETED (7) Execute the query by calling execSQL on sqLiteDatabase and pass the string query SQL_CREATE_WAITLIST_TABLE
41 sqLiteDatabase.execSQL(SQL_CREATE_WAITLIST_TABLE);
42 }
43
44 // COMPLETED (8) Override the onUpgrade method
45 @Override
46 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
47 // For now simply drop the table and create a new one. This means if you change the
48 // DATABASE_VERSION the table will be dropped.
49 // In a production app, this method might be modified to ALTER the table
50 // instead of dropping it, so that existing data is not deleted.
51 // COMPLETED (9) Inside, execute a drop table query, and then call onCreate to re-create it
52 sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + WaitlistEntry.TABLE_NAME);
53 onCreate(sqLiteDatabase);
54 }
55}