@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = " DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
db.execSQL(sql);
}
public ArrayList<Agenda> getAll() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from "+TABLE_NAME, null);
ArrayList<Agenda> agendas = toArrayList(cursor);
db.close();
return agendas;
}
public void delete(int id) {
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_ID + "=?";
String[] whereValue = { Integer.toString(id) };
db.delete(TABLE_NAME, where, whereValue);
db.close();
}
public void update(Agenda a) {
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_ID + "=?";
String[] whereValue = { Integer.toString(a.getId()) };
ContentValues cv = new ContentValues();
cv.put(FIELD_TITLE, a.getTitle());
cv.put(FIELD_CONTENT, a.getContent());
cv.put(FIELD_AGENDATIME, a.getAgendaTime());
cv.put(FIELD_AGENDADATE, a.getAgendaDate());
cv.put(FIELD_ALARMTIME, a.getAlarmTime());
cv.put(FIELD_ALARMDATE, a.getAlarmDate());
cv.put(FIELD_SETALARM, a.getSetAlarm());
cv.put("_typeid", a.getTypeId());
db.update(TABLE_NAME, cv, where, whereValue);
db.close();
}
public long insert(Agenda a) {
SQLiteDatabase db = this.getReadableDatabase();
ContentValues cv = new ContentValues();
cv.put(FIELD_TITLE, a.getTitle());
cv.put(FIELD_CONTENT, a.getContent());
cv.put(FIELD_AGENDATIME, a.getAgendaTime());
cv.put(FIELD_AGENDADATE, a.getAgendaDate());
cv.put(FIELD_ALARMTIME, a.getAlarmTime());
cv.put(FIELD_ALARMDATE, a.getAlarmDate());
cv.put(FIELD_SETALARM, a.getSetAlarm());
cv.put("_typeid", a.getTypeId());
long row = db.insert(TABLE_NAME, null, cv);
db.close();
return row;
}
private ArrayList<Agenda> toArrayList(Cursor c) {
ArrayList<Agenda> arr = new ArrayList<Agenda>();
while (c.moveToNext()) {
Agenda a = new Agenda();
a.setId(c.getInt(0));
a.setTitle(c.getString(1));
a.setContent(c.getString(2));
a.setAgendaTime(c.getString(3));
a.setAgendaDate(c.getString(4));
a.setAlarmTime(c.getString(5));
a.setAgendaDate(c.getString(6));
a.setSetAlarm(c.getInt(7));
a.setTypeId(c.getInt(8));
arr.add(a);
}
return arr;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = " DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
public long insert(AgendaType at) {
SQLiteDatabase db = this.getReadableDatabase();
ContentValues cv = new ContentValues();
cv.put(FIELD_TYPENAME, at.getTypeName());
long row = db.insert(TABLE_NAME, null, cv);
db.close();
return row;
}
public void delete(int id) {
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_TYPEID + "=?";
String[] whereValue = { Integer.toString(id) };
db.delete(TABLE_NAME, where, whereValue);
db.close();
}
public void update(AgendaType at) {
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_TYPEID + "=?";
String[] whereValue = { Integer.toString(at.getTypeId()) };
ContentValues cv = new ContentValues();
cv.put(FIELD_TYPENAME, at.getTypeName());
db.update(TABLE_NAME, cv, where, whereValue);
db.close();
}
public ArrayList<AgendaType> getAll() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
ArrayList<AgendaType> at = toArrayList(cursor);
db.close();
return at;
}
public AgendaType getById(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where "
+ FIELD_TYPEID + "=" + id + "", null);
AgendaType at = new AgendaType();
if (cursor.moveToNext()) {
at.setTypeId(cursor.getInt(0));
at.setTypeName(cursor.getString(1));
}
db.close();
return at;
}
private ArrayList<AgendaType> toArrayList(Cursor c) {
ArrayList<AgendaType> arr = new ArrayList<AgendaType>();
while (c.moveToNext()) {
AgendaType a = new AgendaType();
a.setTypeId(c.getInt(0));
a.setTypeName(c.getString(1));
arr.add(a);
}
return arr;
}
}