/* * Updating a todo */ public int updateToDo(Todo todo) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_TODO, todo.getNote()); values.put(KEY_STATUS, todo.getStatus()); // updating row return db.update( TABLE_TODO, values, KEY_ID + " = ?", new String[] {String.valueOf(todo.getId())}); }
/** getting all todos under single tag */ public List<Todo> getAllToDosByTag(String tag_name) { List<Todo> todos = new ArrayList<Todo>(); String selectQuery = "SELECT * FROM " + TABLE_TODO + " td, " + TABLE_TAG + " tg, " + TABLE_TODO_TAG + " tt WHERE tg." + KEY_TAG_NAME + " = '" + tag_name + "'" + " AND tg." + KEY_ID + " = " + "tt." + KEY_TAG_ID + " AND td." + KEY_ID + " = " + "tt." + KEY_TODO_ID; Log.e(LOG, selectQuery); SQLiteDatabase db = this.getReadableDatabase(); Cursor c = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (c.moveToFirst()) { do { 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))); // adding to todo list todos.add(td); } while (c.moveToNext()); } return todos; }
/* * Creating a todo */ public long createToDo(Todo todo, long[] tag_ids) { SQLiteDatabase db = this.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; }
/* * get single todo */ public Todo getTodo(long todo_id) { SQLiteDatabase db = this.getReadableDatabase(); String selectQuery = "SELECT * FROM " + TABLE_TODO + " WHERE " + KEY_ID + " = " + todo_id; Log.e(LOG, selectQuery); 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; }
/* * Deleting a tag */ public void deleteTag(Tag tag, boolean should_delete_all_tag_todos) { SQLiteDatabase db = this.getWritableDatabase(); // before deleting tag // check if todos under this tag should also be deleted if (should_delete_all_tag_todos) { // get all todos under this tag List<Todo> allTagToDos = getAllToDosByTag(tag.getTagName()); // delete all todos for (Todo todo : allTagToDos) { // delete todo deleteToDo(todo.getId()); } } // now delete the tag db.delete(TABLE_TAG, KEY_ID + " = ?", new String[] {String.valueOf(tag.getId())}); }
/** getting all todos */ public List<Todo> getAllToDos() { List<Todo> todos = new ArrayList<Todo>(); String selectQuery = "SELECT * FROM " + TABLE_TODO; Log.e(LOG, selectQuery); SQLiteDatabase db = this.getReadableDatabase(); Cursor c = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (c.moveToFirst()) { do { 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))); // adding to todo list todos.add(td); } while (c.moveToNext()); } return todos; }