Prints the contents of a Cursor to System.out. - Android Database

Android examples for Database:Cursor

Description

Prints the contents of a Cursor to System.out.

Demo Code

/*// w  w w.j av a 2 s  .c  o  m
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.PrintStream;

import android.database.Cursor;
import android.database.sqlite.SQLiteException;

public class Main {
  /**
   * Prints the contents of a Cursor to System.out. The position is restored
   * after printing.
   *
   * @param cursor
   *          the cursor to print
   */
  public static void dumpCursor(Cursor cursor) {
    dumpCursor(cursor, System.out);
  }

  /**
   * Prints the contents of a Cursor to a PrintSteam. The position is restored
   * after printing.
   *
   * @param cursor
   *          the cursor to print
   * @param stream
   *          the stream to print to
   */
  public static void dumpCursor(Cursor cursor, PrintStream stream) {
    stream.println(">>>>> Dumping cursor " + cursor);
    if (cursor != null) {
      int startPos = cursor.getPosition();

      cursor.moveToPosition(-1);
      while (cursor.moveToNext()) {
        dumpCurrentRow(cursor, stream);
      }
      cursor.moveToPosition(startPos);
    }
    stream.println("<<<<<");
  }

  /**
   * Prints the contents of a Cursor to a StringBuilder. The position is
   * restored after printing.
   *
   * @param cursor
   *          the cursor to print
   * @param sb
   *          the StringBuilder to print to
   */
  public static void dumpCursor(Cursor cursor, StringBuilder sb) {
    sb.append(">>>>> Dumping cursor " + cursor + "\n");
    if (cursor != null) {
      int startPos = cursor.getPosition();

      cursor.moveToPosition(-1);
      while (cursor.moveToNext()) {
        dumpCurrentRow(cursor, sb);
      }
      cursor.moveToPosition(startPos);
    }
    sb.append("<<<<<\n");
  }

  /**
   * Prints the contents of a Cursor's current row to System.out.
   *
   * @param cursor
   *          the cursor to print from
   */
  public static void dumpCurrentRow(Cursor cursor) {
    dumpCurrentRow(cursor, System.out);
  }

  /**
   * Prints the contents of a Cursor's current row to a PrintSteam.
   *
   * @param cursor
   *          the cursor to print
   * @param stream
   *          the stream to print to
   */
  public static void dumpCurrentRow(Cursor cursor, PrintStream stream) {
    String[] cols = cursor.getColumnNames();
    stream.println("" + cursor.getPosition() + " {");
    int length = cols.length;
    for (int i = 0; i < length; i++) {
      String value;
      try {
        value = cursor.getString(i);
      } catch (SQLiteException e) {
        // assume that if the getString threw this exception then the column is
        // not
        // representable by a string, e.g. it is a BLOB.
        value = "<unprintable>";
      }
      stream.println("   " + cols[i] + '=' + value);
    }
    stream.println("}");
  }

  /**
   * Prints the contents of a Cursor's current row to a StringBuilder.
   *
   * @param cursor
   *          the cursor to print
   * @param sb
   *          the StringBuilder to print to
   */
  public static void dumpCurrentRow(Cursor cursor, StringBuilder sb) {
    String[] cols = cursor.getColumnNames();
    sb.append("" + cursor.getPosition() + " {\n");
    int length = cols.length;
    for (int i = 0; i < length; i++) {
      String value;
      try {
        value = cursor.getString(i);
      } catch (SQLiteException e) {
        // assume that if the getString threw this exception then the column is
        // not
        // representable by a string, e.g. it is a BLOB.
        value = "<unprintable>";
      }
      sb.append("   " + cols[i] + '=' + value + "\n");
    }
    sb.append("}\n");
  }
}

Related Tutorials