dump Current Column from Cursor - Android Database

Android examples for Database:Cursor Column

Description

dump Current Column from Cursor

Demo Code

/* //  w w  w.  j a v  a  2 s .com
 * Copyright (C) 2013 The Drycake 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.
 */
//package com.java2s;
import android.database.Cursor;

public class Main {
    public static void dumpCurrentColumn(Cursor cursor, String columnName,
            StringBuilder sb) {
        if (cursor == null || sb == null) {
            return;
        }
        int columnIndex = cursor.getColumnIndex(columnName);
        if (columnIndex != -1) {
            sb.append(columnName);
            sb.append("=");
            if (cursor.isNull(columnIndex)) {
                sb.append("null");
            } else {
                try {
                    sb.append(cursor.getString(columnIndex));
                } catch (Exception e) {
                    sb.append("unprintable");
                }
            }
        }
    }

    public static String getString(Cursor cursor, String columnName,
            String defaultValue) {
        int columnIndex = cursor != null ? cursor
                .getColumnIndex(columnName) : -1;
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            return cursor.getString(columnIndex);
        } else {
            return defaultValue;
        }
    }
}

Related Tutorials