Increased DB version. Changed User to HomeOwner in existing DBs

This commit is contained in:
Mary Tran 2018-10-30 12:24:46 -04:00
parent 8b2532d27e
commit 23293089cb

View file

@ -22,7 +22,7 @@ import java.util.List;
public class DBHelper extends SQLiteOpenHelper { public class DBHelper extends SQLiteOpenHelper {
//version of db used for update method //version of db used for update method
private static final int DB_VERSION = 1; private static final int DB_VERSION = 2;
//name of db in app data //name of db in app data
private static final String DB_NAME = "UsersDB.db"; private static final String DB_NAME = "UsersDB.db";
@ -58,35 +58,46 @@ public class DBHelper extends SQLiteOpenHelper {
*/ */
public DBHelper(Context context){ public DBHelper(Context context){
super(context, DB_NAME, null, DB_VERSION); super(context, DB_NAME, null, DB_VERSION);
//since these methods take a while we will call them once and store the returned dbs
readDB = this.getReadableDatabase(); readDB = this.getReadableDatabase();
writeDB = this.getWritableDatabase(); writeDB = this.getWritableDatabase();
//pre-add the admin user
addUser(new Admin()); addUser(new Admin());
} }
@Override @Override
public void onCreate(SQLiteDatabase db){ public void onCreate(SQLiteDatabase db){
//making the table containing user login information
String CREATE_LOGIN_TABLE = "CREATE TABLE "+ TABLE_LOGIN + "(" String CREATE_LOGIN_TABLE = "CREATE TABLE "+ TABLE_LOGIN + "("
+ COLUMN_USERNAME + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK," + COLUMN_USERNAME + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK,"
+ COLUMN_PASSWORD + " TEXT NOT NULL," + COLUMN_PASSWORD + " TEXT NOT NULL,"
+ COLUMN_FIRSTNAME + " TEXT DEFAULT 'FirstName'," + COLUMN_FIRSTNAME + " TEXT DEFAULT 'FirstName',"
+ COLUMN_LASTNAME + " TEXT DEFAULT 'LastName'," + COLUMN_LASTNAME + " TEXT DEFAULT 'LastName',"
+ COLUMN_USERTYPE + " TEXT NOT NULL" + ")"; + COLUMN_USERTYPE + " TEXT NOT NULL" + ")";
db.execSQL(CREATE_LOGIN_TABLE); db.execSQL(CREATE_LOGIN_TABLE);
//making the table containing services and their rates
String CREATE_SERVICES_TABLE = "CREATE TABLE "+ TABLE_SERVICES + "(" String CREATE_SERVICES_TABLE = "CREATE TABLE "+ TABLE_SERVICES + "("
+ COLUMN_SERVICE + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK," + COLUMN_SERVICE + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK,"
+ COLUMN_RATE + " REAL DEFAULT 0.0" + ")"; + COLUMN_RATE + " REAL DEFAULT 0.0" + ")";
db.execSQL(CREATE_SERVICES_TABLE); db.execSQL(CREATE_SERVICES_TABLE);
} }
@Override @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN); switch(oldVersion){
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SERVICES); case 1: //going from db version 1 to 2
onCreate(db); //change usertype of Users to Homeowner
ContentValues values = new ContentValues();
values.put(COLUMN_USERTYPE, "HomeOwner");
db.update(TABLE_LOGIN, values, COLUMN_USERTYPE + " = ?", new String[]{"User"});
//if services table is not created, create it
db.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE_SERVICES + "("
+ COLUMN_SERVICE + " TEXT UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK,"
+ COLUMN_RATE + " REAL DEFAULT 0.0" + ")");
}
} }
@Override @Override