CommentsDataSource.java (Cont.)

cursor.moveToFirst( );
Move the cursor to the first row. This method will return false if the cursor is empty.

System.out.println( "Comment deleted with id: " + id );
This command is for the debugging purpose and it prints the string to the log instead of smartphone interface. You can then view the log in Eclipse by picking the following options:
   Window ⇒ Show View ⇒ Android ⇒ LogCat
It is a good habit of looking at the logcat output as that is also where the stack traces of any uncaught exceptions are displayed. The following figure is a snapshot of the logcat in Eclipse:


It is an INFO log message. Other log messages, such as d (debug), e (error), v (verbose), and w (warning), can be sent as an example from the MySQLiteHelper class:
   Log.w( MySQLiteHelper.class.getName( ),
     "Upgrading database from version "
     + oldVersion + " to " + newVersion
     + ", which will destroy all old data" );
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;
  }
}




      It’ll be a cold day in July when our team wins the championship.    
      We’re terrible.