Ejemplo n.º 1
0
  public boolean update(Book book) {
    boolean updated = false;

    try {
      open();

      ContentValues cv = new ContentValues();
      cv.put(DBHelper.KEY_TITLE, book.getTitle());
      cv.put(DBHelper.KEY_AUTHOR, book.getAuthor());

      updated =
          getDB()
                  .update(
                      DBHelper.TABLE_BOOKS,
                      cv,
                      DBHelper.KEY_ID + " = ?",
                      new String[] {String.valueOf(book.getId())})
              > 0;

      close();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      close();
    }

    return updated;
  }
Ejemplo n.º 2
0
  private Book CursorToBook(Cursor c) {
    Book book = new Book();

    book.setId(c.getInt(c.getColumnIndex(DBHelper.KEY_ID)));
    book.setTitle(c.getString(c.getColumnIndex(DBHelper.KEY_TITLE)));
    book.setAuthor(c.getString(c.getColumnIndex(DBHelper.KEY_AUTHOR)));

    return book;
  }
Ejemplo n.º 3
0
  public boolean insert(Book book) {
    boolean inserted = false;

    try {
      open();

      ContentValues contentValues = new ContentValues();
      contentValues.put(DBHelper.KEY_TITLE, book.getTitle());
      contentValues.put(DBHelper.KEY_AUTHOR, book.getAuthor());

      long id = getDB().insert(DBHelper.TABLE_BOOKS, null, contentValues);

      inserted = -1 != id;

      close();

    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      close();
    }

    return inserted;
  }
Ejemplo n.º 4
0
  public boolean delete(Book book) {
    boolean deleted = false;

    try {
      open();
      String whereClause = DBHelper.KEY_ID + " = " + book.getId();
      deleted = getDB().delete(DBHelper.TABLE_BOOKS, whereClause, null) > 0;
      close();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      close();
    }

    return deleted;
  }