可能有用。
public boolean deleteAllFood() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
int affectedRows = db.delete(DBHelper.TABLE_NAME_FOOD, null, null);
return affectedRows > 0;
}
public class DBProgram {
private static DBProgram INSTANCE;
private Context context;
private DBHelper dbHelper;
private DBProgram(Context context) {
// burda bu methodu kullanan activity ile eileştiriyoruz
this.dbHelper = new DBHelper(context);
}
public static synchronized DBProgram getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new DBProgram(context);
}
return INSTANCE;
}
//**********************************************
public boolean updateById(ProgramModel program) {
SQLiteDatabase database = dbHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DBHelper.COLUM_NAME_P, program.getProgName());
contentValues.put(DBHelper.COLUM_DAY_P, program.getDay());
contentValues.put(DBHelper.COLUMN_WEIGHT_P, program.getWeight());
contentValues.put(DBHelper.COLUMN_SET_P, program.getSet());
contentValues.put(DBHelper.COLUMN_REPETITION_P, program.getRepetition());
int affectedRows = database.update(DBHelper.TABLE_NAME_PROGRAM, contentValues, "PROG_ID_P = ?", new String[]{String.valueOf(program.getId())});
return affectedRows > 0;
}
//**********************************************
//**********************************************
// TODO
public boolean deleteProgramById(int id) {
SQLiteDatabase database = dbHelper.getReadableDatabase();
int affectedRows = database.delete(DBHelper.TABLE_NAME_PROGRAM, DBHelper.COLUMN_ID_P + "= ?", new String[]{String.valueOf(id)});
// return bize etkilenen sıra sayısınıını temsil eder
return affectedRows > 0;
}
//**********************************************
//***************************************************
public boolean deleteProgramByName(String progName) {
SQLiteDatabase database = dbHelper.getReadableDatabase();
final String whereClause = DBHelper.COLUM_NAME_P + "=?";
final String whereArgs[] = {progName};
int affectedRows = database.delete(DBHelper.TABLE_NAME_PROGRAM, whereClause, whereArgs);
return affectedRows > 0;
}
//***************************************************
//************************************** get Meal
// TODO WEB Get All Meals
public List<ProgramModel> getAllProgram(String name) {
List<ProgramModel> foodList = new ArrayList<>();
ProgramModel food;
SQLiteDatabase database = dbHelper.getReadableDatabase();
final String kolonlar[] = {DBHelper.COLUMN_ID_P,
DBHelper.COLUM_NAME_P,
DBHelper.COLUM_DAY_P,
DBHelper.COLUMN_WEIGHT_P,
DBHelper.COLUMN_SET_P,
DBHelper.COLUMN_REPETITION_P};
final String whereClause = DBHelper.COLUM_DAY_P + "=?";
final String whereArgs[] = {name};
Cursor cursor = database.query(DBHelper.TABLE_NAME_PROGRAM, kolonlar, whereClause, whereArgs, null, null, null);
while (cursor.moveToNext()) {
food = new ProgramModel();
food.setId(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_ID_P)));
food.setProgName(cursor.getString(cursor.getColumnIndex(DBHelper.COLUM_NAME_P)));
food.setDay(cursor.getString(cursor.getColumnIndex(DBHelper.COLUM_DAY_P)));
food.setWeight(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_WEIGHT_P)));
food.setSet(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_SET_P)));
food.setRepetition(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_REPETITION_P)));
foodList.add(food);
}
database.close();
cursor.close();
return foodList;
}
//**************************************
//**************************************insert FOOD
//TODO LOCAL insert Foods
public boolean insertProgram(ProgramModel favorite) {
boolean result = false;
ContentValues contentValues = new ContentValues();
contentValues.put(DBHelper.COLUM_NAME_P, favorite.getProgName());
contentValues.put(DBHelper.COLUM_DAY_P, favorite.getDay());
contentValues.put(DBHelper.COLUMN_WEIGHT_P, favorite.getWeight());
contentValues.put(DBHelper.COLUMN_SET_P, favorite.getSet());
contentValues.put(DBHelper.COLUMN_REPETITION_P, favorite.getRepetition());
SQLiteDatabase database = dbHelper.getWritableDatabase();
long id = database.insert(DBHelper.TABLE_NAME_PROGRAM, null, contentValues);
if (id != 1) {
result = true;
}
database.close();
return result;
}
//***************************************************
// ******************************* SQLITE HELPER CLASS ******************
private class DBHelper extends SQLiteOpenHelper {
private final Context context;
private static final String DATABASE_NAME = "PROGRAM_INFO";
private static final String TABLE_NAME_PROGRAM = "PROGRAM";
private static final int DATABASE_VERSION = 2;
// FOOD
private static final String COLUMN_ID_P = "PROG_ID_P";
private static final String COLUM_NAME_P = "PROG_NAME_P";
private static final String COLUM_DAY_P = "PROG_DAY_P";
private static final String COLUMN_WEIGHT_P = "PROG_WEIGHT_P";
private static final String COLUMN_SET_P = "PROG_SET_P";
private static final String COLUMN_REPETITION_P = "PROG_REPETITION_P";
private final String CREATE_TABLE_PROGRAM = "CREATE TABLE " + TABLE_NAME_PROGRAM +
" (" + COLUMN_ID_P + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUM_NAME_P + " TEXT, "
+ COLUM_DAY_P + " TEXT, "
+ COLUMN_WEIGHT_P + " INTEGER, "
+ COLUMN_SET_P + " INTEGER, "
+ COLUMN_REPETITION_P + " INTEGER)";
private static final String DROP_TABLE_PROGRAM = "DROP TABLE IF EXIST " + TABLE_NAME_PROGRAM;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_PROGRAM);
Util.showMessage(context, "Database Created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE_PROGRAM);
Util.showMessage(context, "Database Upgrated");
onCreate(db);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onDowngrade(db, oldVersion, newVersion);
}
}
}