1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
public class Table1 {
private static final String TABLE_TODO = "todos";
private static final String KEY_ID = "id";
private static final String KEY_CREATED_AT = "created_at";
private static final String KEY_TODO = "todo";
private static final String KEY_STATUS = "status";
private static final String CREATE_TABLE= "CREATE TABLE "
+ TABLE_TODO + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TODO
+ " TEXT," + KEY_STATUS + " INTEGER," + KEY_CREATED_AT
+ " DATETIME" + ")";
private DataBaseHelper m_dbh;
public Table1(DataBaseHelper dbh) {
m_dbh = dbh;
}
public long createToDo(Todo todo, long[] tag_ids) {
SQLiteDatabase db = m_dbh.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TODO, todo.getNote());
values.put(KEY_STATUS, todo.getStatus());
values.put(KEY_CREATED_AT, getDateTime());
// insert row
long todo_id = db.insert(TABLE_TODO, null, values);
// insert tag_ids
for (long tag_id : tag_ids) {
createTodoTag(todo_id, tag_id);
}
return todo_id;
}
public Todo getTodo(long todo_id) {
SQLiteDatabase db = m_dbh.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_TODO + " WHERE "
+ KEY_ID + " = " + todo_id;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null)
c.moveToFirst();
Todo td = new Todo();
td.setId(c.getInt(c.getColumnIndex(KEY_ID)));
td.setNote((c.getString(c.getColumnIndex(KEY_TODO))));
td.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));
return td;
}
} |
Partager