Example usage for android.database Cursor getColumnNames

List of usage examples for android.database Cursor getColumnNames

Introduction

In this page you can find the example usage for android.database Cursor getColumnNames.

Prototype

String[] getColumnNames();

Source Link

Document

Returns a string array holding the names of all of the columns in the result set in the order in which they were listed in the result.

Usage

From source file:org.getlantern.firetweet.util.content.DatabaseUpgradeHelper.java

private static String[] getColumnNames(final SQLiteDatabase db, final String table) {
    final Cursor cur = db.query(table, null, null, null, null, null, null, "1");
    if (cur == null)
        return null;
    try {//www  . j  a va 2s  .co  m
        return cur.getColumnNames();
    } finally {
        cur.close();
    }
}

From source file:com.android.ex.chips.RecipientAlternatesAdapter.java

/**
 * @return a new cursor based on the given cursor with all duplicate destinations removed.
 *         It's only intended to use for the alternate list, so...
 *         - This method ignores all other fields and dedupe solely on the destination. Normally,
 *         if a cursor contains multiple contacts and they have the same destination, we'd still want
 *         to show both.//from   ww w.j a v a  2  s.c  o m
 *         - This method creates a MatrixCursor, so all data will be kept in memory. We wouldn't want
 *         to do this if the original cursor is large, but it's okay here because the alternate list
 *         won't be that big.
 */
// Visible for testing
/* package */static Cursor removeDuplicateDestinations(Cursor original) {
    final MatrixCursor result = new MatrixCursor(original.getColumnNames(), original.getCount());
    final HashSet<String> destinationsSeen = new HashSet<String>();

    original.moveToPosition(-1);
    while (original.moveToNext()) {
        final String destination = original.getString(Query.DESTINATION);
        if (destinationsSeen.contains(destination)) {
            continue;
        }
        destinationsSeen.add(destination);

        Object[] cursorObjects;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            cursorObjects = new Object[] { original.getString(Query.NAME),
                    original.getString(Query.DESTINATION), original.getInt(Query.DESTINATION_TYPE),
                    original.getString(Query.DESTINATION_LABEL), original.getLong(Query.CONTACT_ID),
                    original.getLong(Query.DATA_ID), original.getString(Query.PHOTO_THUMBNAIL_URI),
                    original.getInt(Query.DISPLAY_NAME_SOURCE) };
        } else {
            cursorObjects = new Object[] { original.getString(Query.NAME),
                    original.getString(Query.DESTINATION), original.getInt(Query.DESTINATION_TYPE),
                    original.getString(Query.DESTINATION_LABEL), original.getLong(Query.CONTACT_ID),
                    original.getLong(Query.DATA_ID), original.getString(Query.PHOTO_ID) };
        }
        result.addRow(cursorObjects);
    }

    return result;
}

From source file:org.thinschema.dataaccess.JSONAdapter.java

/**
 * Get all data from table and convert them to JSON. Each row is converted
 * to its own JSONObject containing the names of the columns as keys, and
 * the contents as values. For example, if a table whose name is "People"
 * consists of two columns "FirstName" and "LastName", the generated JSON
 * would be://from   w  w  w .  j  a  v a2  s  . c o  m
 * </p>
 * <code>
 * { "name": "People", "rows": [ { "FirstName": "John", "LastName": "Doe" },
 * { "FirstName": "Susan", "LastName": "Appleseed" } ] }
 * </code>
 *
 * @param database  SQLiteDatabase instance.
 * @param tableName The name of the table.
 * @return JSONObject instance containing all records.
 */
public static JSONObject get(SQLiteDatabase database, String tableName) {

    JSONObject retval = new JSONObject();
    Cursor cursor = null;
    try {
        retval.put("name", tableName);
        cursor = database.query(tableName, null, null, null, null, null, null);

        // we get the list of all column names to make it easier when inserting key-value pairs
        String[] columnNames = cursor.getColumnNames();

        // iterate through each row
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            JSONArray data = new JSONArray();
            while (!cursor.isAfterLast()) {
                JSONObject row = new JSONObject();

                // for each column, store the key-value pair, using column name as key
                for (int i = 0, size = columnNames.length; i < size; ++i) {
                    // convert everything to a string
                    row.put(columnNames[i], cursor.getString(i));
                }

                data.put(row);
            }

            // add all of that to the result
            retval.put("rows", data);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return retval;
}

From source file:cm.confide.ex.chips.RecipientAlternatesAdapter.java

/**
 * @return a new cursor based on the given cursor with all duplicate destinations removed.
 *
 * It's only intended to use for the alternate list, so...
 * - This method ignores all other fields and dedupe solely on the destination.  Normally,
 * if a cursor contains multiple contacts and they have the same destination, we'd still want
 * to show both.//from   w w  w.  j  a v a 2  s  . com
 * - This method creates a MatrixCursor, so all data will be kept in memory.  We wouldn't want
 * to do this if the original cursor is large, but it's okay here because the alternate list
 * won't be that big.
 */
// Visible for testing
/* package */ static Cursor removeDuplicateDestinations(Cursor original) {
    final MatrixCursor result = new MatrixCursor(original.getColumnNames(), original.getCount());
    final HashSet<String> destinationsSeen = new HashSet<String>();

    original.moveToPosition(-1);
    while (original.moveToNext()) {
        final String destination = original.getString(Query.DESTINATION);
        if (destinationsSeen.contains(destination)) {
            continue;
        }
        destinationsSeen.add(destination);

        result.addRow(new Object[] { original.getString(Query.NAME), original.getString(Query.DESTINATION),
                original.getInt(Query.DESTINATION_TYPE), original.getString(Query.DESTINATION_LABEL),
                original.getLong(Query.CONTACT_ID), original.getLong(Query.DATA_ID),
                original.getString(Query.PHOTO_THUMBNAIL_URI), original.getInt(Query.DISPLAY_NAME_SOURCE) });
    }

    return result;
}

From source file:com.chess.genesis.data.GameDataDB.java

public static Bundle rowToBundle(final Cursor cursor, final int index, final boolean closeCursor) {
    final Bundle bundle = new Bundle();
    final String[] column = cursor.getColumnNames();

    cursor.moveToPosition(index);//  ww  w .ja v  a 2s  . c o m
    for (int i = 0, len = cursor.getColumnCount(); i < len; i++)
        bundle.putString(column[i], cursor.getString(i));
    if (closeCursor)
        cursor.close();
    return bundle;
}

From source file:org.linphone.compatibility.ApiFivePlus.java

public static Cursor getGeneralContactCursor(ContentResolver cr, String select, boolean shouldGroupBy) {
    String[] projection = new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME };
    String query;//from   w  w w  .j  a va  2s .  co  m

    query = Data.DISPLAY_NAME + " IS NOT NULL AND (" + select + ")";

    Cursor cursor = cr.query(Data.CONTENT_URI, projection, query, null,
            " lower(" + Data.DISPLAY_NAME + ") COLLATE UNICODE ASC");

    if (!shouldGroupBy || cursor == null) {
        return cursor;
    }

    MatrixCursor result = new MatrixCursor(cursor.getColumnNames());
    Set<String> groupBy = new HashSet<String>();
    while (cursor.moveToNext()) {
        String name = cursor.getString(getCursorDisplayNameColumnIndex(cursor));
        if (!groupBy.contains(name)) {
            groupBy.add(name);
            Object[] newRow = new Object[cursor.getColumnCount()];

            int contactID = cursor.getColumnIndex(Data.CONTACT_ID);
            int displayName = cursor.getColumnIndex(Data.DISPLAY_NAME);

            newRow[contactID] = cursor.getString(contactID);
            newRow[displayName] = cursor.getString(displayName);

            result.addRow(newRow);
        }
    }
    cursor.close();
    return result;
}

From source file:android.database.DatabaseUtils.java

/**
 * Prints the contents of a Cursor's current row to a StringBuilder.
 *
 * @param cursor the cursor to print//www .j  a  va  2 s  .  co m
 * @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");
}

From source file:android.database.DatabaseUtils.java

/**
 * Prints the contents of a Cursor's current row to a PrintSteam.
 *
 * @param cursor the cursor to print/*from w  w  w  .ja va2  s .com*/
 * @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("}");
}

From source file:org.chromium.chrome.browser.util.CompatibilityFileProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Cursor source = super.query(uri, projection, selection, selectionArgs, sortOrder);

    String[] columnNames = source.getColumnNames();
    String[] newColumnNames = columnNamesWithData(columnNames);
    if (columnNames == newColumnNames)
        return source;

    MatrixCursor cursor = new MatrixCursor(newColumnNames, source.getCount());

    source.moveToPosition(-1);// w w w . jav a 2 s . c  o m
    while (source.moveToNext()) {
        MatrixCursor.RowBuilder row = cursor.newRow();
        for (int i = 0; i < columnNames.length; i++) {
            switch (source.getType(i)) {
            case Cursor.FIELD_TYPE_INTEGER:
                row.add(source.getInt(i));
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                row.add(source.getFloat(i));
                break;
            case Cursor.FIELD_TYPE_STRING:
                row.add(source.getString(i));
                break;
            case Cursor.FIELD_TYPE_BLOB:
                row.add(source.getBlob(i));
                break;
            case Cursor.FIELD_TYPE_NULL:
            default:
                row.add(null);
                break;
            }
        }
    }

    source.close();
    return cursor;
}

From source file:com.zegoggles.smssync.mail.MessageConverter.java

private Map<String, String> getMessageMap(Cursor cursor) {
    final String[] columns = cursor.getColumnNames();
    final Map<String, String> msgMap = new HashMap<String, String>(columns.length);
    for (String column : columns) {
        String value;//ww w.j  a  va 2s . co m
        try {
            final int index = cursor.getColumnIndex(column);
            if (index != -1) {
                value = cursor.getString(index);
            } else {
                continue;
            }
        } catch (SQLiteException ignored) {
            // this can happen in case of BLOBS in the DB
            // column type checking is API level >= 11
            value = "[BLOB]";
        }
        msgMap.put(column, value);
    }
    return msgMap;
}