CommentsDataSource.java (Cont.)

database.delete( MySQLiteHelper.TABLE_COMMENTS,
      MySQLiteHelper.COLUMN_ID + " = " + id, null );
The method delete(table,whereClause,whereArgs) is to delete rows in the database. Two parameters of the method are

  • table, which is the table to delete from, and
  • whereClause, which is the optional WHERE clause to apply when deleting. Passing null will delete all rows.

The result SQL command is as follows:
   delete from  comments  where  _id = id;

List<Comment> comments = new ArrayList<Comment>( );
A List of Comment is written as List<Comment> where List is a generic interface that takes a type parameter — in this case, Comment.

while ( !cursor.isAfterLast( ) ) {
Return whether the cursor is pointing to the position after the last row.

cursor.moveToNext( );
Move the cursor to the next row. This method will return false if the cursor is already past the last entry in the result set.

cursor.close( );
Close the Cursor, releasing all of its resources and making it invalid.

comment.setId( cursor.getLong( 0 ) );
The method getLong(int columnIndex) returns the value of the requested column as a long where

    columnIndex: the zero-based index of the target column. For example, 0 is the first column.
src/main/java/com/example/wenchen/sqlitedemo/CommentsDataSource.java
package com.example.wenchen.sqlitedemo;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CommentsDataSource {

  // Database fields
  private SQLiteDatabase database;
  private MySQLiteHelper dbHelper;
  private String[ ] allColumns = { MySQLiteHelper.COLUMN_ID,
    MySQLiteHelper.COLUMN_COMMENT };

  public CommentsDataSource( Context context ) {
    dbHelper = new MySQLiteHelper( context );
  }

  public void open( ) throws SQLException {
    database = dbHelper.getWritableDatabase( );
  }

  public void close( ) {
    dbHelper.close( );
  }

  public Comment createComment( String comment ) {
    // insert into  comments  values( 'Very nice' );
    ContentValues values = new ContentValues( );
    values.put( MySQLiteHelper.COLUMN_COMMENT, comment );
    long insertId = database.insert( MySQLiteHelper.TABLE_COMMENTS, null, values );

    // select * from  comments  where  _id = insertId;
    Cursor cursor = database.query( MySQLiteHelper.TABLE_COMMENTS, allColumns,
      MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null, null );
    cursor.moveToFirst( );
    return cursorToComment( cursor );
  }

  public void deleteComment( Comment comment ) {
    // delete from  comments  where  _id = id;
    long id = comment.getId( );
    System.out.println( "Comment deleted with id: " + id );
    database.delete( MySQLiteHelper.TABLE_COMMENTS,
      MySQLiteHelper.COLUMN_ID + " = " + id, null );
  }

  public List<Comment> getAllComments( ) {
    // select  _id, comment  from  comments;
    List<Comment> comments = new ArrayList<Comment>( );
    Cursor cursor = database.query( MySQLiteHelper.TABLE_COMMENTS,
      allColumns, null, null, null, null, null );
    cursor.moveToFirst( );
    while ( !cursor.isAfterLast( ) ) {
      Comment comment = cursorToComment( cursor );
      comments.add( comment );
      cursor.moveToNext( );
    }
    // Make sure to close the cursor.
    cursor.close( );
    return comments;
  }

  private Comment cursorToComment( Cursor cursor ) {
    Comment comment = new Comment( );
    comment.setId( cursor.getLong( 0 ) );
    comment.setComment( cursor.getString( 1 ) );
    return comment;
  }
}




      “Nowadays most people die of a sort of creeping common sense,    
      and discover when it is too late that the only things    
      one never regrets are one’s mistakes.”    
      ― Oscar Wilde, The Picture of Dorian Gray