CommentsDataSource.java (Cont.)

database = dbHelper.getWritableDatabase( );
Create and/or open a database that will be used for reading and writing. The first time this is called, the database will be opened and the following methods will be called:

ContentValues values = new ContentValues( );
It is used to store a set of values for insertion.

values.put(
      MySQLiteHelper.COLUMN_COMMENT, comment );
Add a value to the set values where the first parameter is the key and the other parameter is the value.

long insertId = database.insert(
      MySQLiteHelper.TABLE_COMMENTS, null, values );
The insert(table,nullColumnHack,values) method inserts a row into the database. It returns the row ID of the newly inserted row, or -1 if an error occurred . The method has the following parameters:

  • table, which is the table to insert the row into

  • nullColumnHack, which is optional; may be null. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a null into.

  • values, which is a map that contains the column values for the row. The keys are the column names and the values the column values.

One example of the result SQL commands is as follows:
   insert into  comments  values( 'Very nice' );
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;
  }
}




      “I think I’ve discovered the secret of life —    
      you just hang around until you get used to it.”    
      ― Charles Schultz