Prints the contents of a Cursor's current row to System.out. - Android Database

Android examples for Database:Cursor

Description

Prints the contents of a Cursor's current row to System.out.

Demo Code

/*//from ww w .ja va  2  s.c om
 * 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'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