Manage our Schema
Next, we need to flesh out the onCreate() and onUpgrade() methods in RestaurantHelper, to actually create the schema we want.
To do this, add an import for android.database.Cursor and use the following implementation of onCreate():
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE restaurants (_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT, address TEXT, type TEXT, notes TEXT);");
}
Here, we are simply executing a SQL statement to create a restaurant table with a particular schema.
For onUpgrade(), there is nothing we really need to do now, since this method will not be executed until we have at least two schema versions. So far, we barely have our first schema version. So, just put a comment to that effect in onUpgrade(), perhaps something like this:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// no-op, since will not be called until 2nd schema
// version exists
}
In a production system, of course, we would want to make a temporary table, copy our current data to it, fix up the real table's schema, then migrate the data back.
To do this, add an import for android.database.Cursor and use the following implementation of onCreate():
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE restaurants (_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT, address TEXT, type TEXT, notes TEXT);");
}
Here, we are simply executing a SQL statement to create a restaurant table with a particular schema.
For onUpgrade(), there is nothing we really need to do now, since this method will not be executed until we have at least two schema versions. So far, we barely have our first schema version. So, just put a comment to that effect in onUpgrade(), perhaps something like this:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// no-op, since will not be called until 2nd schema
// version exists
}
In a production system, of course, we would want to make a temporary table, copy our current data to it, fix up the real table's schema, then migrate the data back.
No comments:
Post a Comment