I've just started programming in android a few months ago and am confused about when android actually creates the SQL database file. The dbhelper is referenced in multiple activities and I don't want the database to be referenced before it's created. Should I instantiate it in my main activity? I've created a helper/contract class to handle most of the SQL operation as follows:
public class PantryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pantry);
PantryDbHelper pantryDb = new PantryDbHelper(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_pantry, menu);
return true;
}
//The helper class for the SQL table
public class PantryDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Pantry.db";
public PantryDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(PantryContract.SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
//TODO add code to handle upgrades, import table over async task
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
//TODO add write/read methods for SQL database
//contract class for the SQL database, used to identify columns/set up tables
public final class PantryContract {
public PantryContract() {
}
public abstract class PantryColumns implements BaseColumns {
//Column identifiers to be used in each table
public static final String TABLE_NAME = "Pantry";
public static final String COLUMN_NAME_FOOD_ITEM = "FoodItem";
public static final String COLUMN_NAME_FOOD_TYPE = "FoodType";
public static final String COLUMN_NAME_FOOD_AMOUNT = "Amount";
public static final String COLUMN_NAME_EXPIRATION_DATE = "ExpirationDate";
/*
public static final String COLUMN_NAME_CALORIES = "Calories";
public static final String COLUMN_NAME_CARBOHYDRATES = "Carbohydrates";
public static final String COLUMN_NAME_FAT = "Fat";
public static final String COLUMN_NAME_PROTEIN = "Protein";
*/
}
//Table data types/comma
private static final String TEXT_TYPE = " TEXT";
private static final String INTEGER_TYPE = " INTEGER";
private static final String COMMA_SEP = ", ";
private static final String PRIMARY_KEY = "PRIMARY KEY";
private static final String UNIQUE_CONSTRAINT = " Unique";
private static final String NOT_NULL_CONSTRAINT = " NOT NULL";
//Table creation parameters
private static final String SQL_CREATE_ENTRIES = "Create Table "+ PantryColumns.TABLE_NAME + " (" +
PantryColumns._ID + INTEGER_TYPE + PRIMARY_KEY + COMMA_SEP +
PantryColumns.COLUMN_NAME_FOOD_ITEM + TEXT_TYPE + UNIQUE_CONSTRAINT + COMMA_SEP +
PantryColumns.COLUMN_NAME_FOOD_TYPE + TEXT_TYPE + COMMA_SEP +
PantryColumns.COLUMN_NAME_FOOD_AMOUNT + TEXT_TYPE + COMMA_SEP +
PantryColumns.COLUMN_NAME_EXPIRATION_DATE + TEXT_TYPE + " )";
/*
PantryColumns.COLUMN_NAME_CALORIES + INTEGER_TYPE + COMMA_SEP +
PantryColumns.COLUMN_NAME_CARBOHYDRATES + INTEGER_TYPE + COMMA_SEP +
PantryColumns.COLUMN_NAME_FAT + INTEGER_TYPE + COMMA_SEP +
PantryColumns.COLUMN_NAME_PROTEIN + INTEGER_TYPE
*/
}
}
}
Aucun commentaire:
Enregistrer un commentaire