Skip to content

Instantly share code, notes, and snippets.

@jezinka
Created March 20, 2017 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jezinka/0f08f97de42c5cbd94220435ece8a31d to your computer and use it in GitHub Desktop.
Save jezinka/0f08f97de42c5cbd94220435ece8a31d to your computer and use it in GitHub Desktop.
public class MealContract extends BaseTable implements BaseColumns {
String COLUMN_NAME_NAME;
private String SQL_GET_ALL_RECORD;
public MealContract() {
this.TABLE_NAME = "meal";
this.COLUMN_NAME_NAME = "name";
this.SQL_CREATE_ENTRIES = "CREATE TABLE " + TABLE_NAME + " (" +
_ID + " INTEGER PRIMARY KEY," +
COLUMN_NAME_NAME + " TEXT)";
this.SQL_GET_ALL_RECORD = "select " + this.COLUMN_NAME_NAME + " from " + this.TABLE_NAME + " order by " + this.COLUMN_NAME_NAME;
}
public boolean insertMeal(Context context, String name) {
String tableName = this.getTableName();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
CoNaObiadDbHelper helper = new CoNaObiadDbHelper(context);
helper.insertValuesDbHelper(tableName, contentValues);
return true;
}
public ArrayList<String> getAllMeals(SQLiteOpenHelper helper) {
ArrayList<String> array_list = new ArrayList<String>();
SQLiteDatabase db = helper.getReadableDatabase();
Cursor res = db.rawQuery(this.SQL_GET_ALL_RECORD, null);
if (res != null && res.getCount() > 0) {
res.moveToFirst();
do {
String mealName = res.getString(res.getColumnIndex(this.COLUMN_NAME_NAME));
array_list.add(mealName);
} while (res.moveToNext());
}
db.close();
return array_list;
}
public boolean isAnyMealSaved(SQLiteOpenHelper helper) {
SQLiteDatabase db = helper.getReadableDatabase();
return queryNumEntries(db, "meal") > 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment