Example usage for android.database Cursor getColumnName

List of usage examples for android.database Cursor getColumnName

Introduction

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

Prototype

String getColumnName(int columnIndex);

Source Link

Document

Returns the column name at the given zero-based column index.

Usage

From source file:org.disrupted.rumble.database.statistics.StatisticDatabase.java

public JSONArray getJSON() {
    SQLiteDatabase database = databaseHelper.getReadableDatabase();
    Cursor cursor = database.query(getTableName(), null, null, null, null, null, null);
    if (cursor == null)
        return null;
    JSONArray resultSet = new JSONArray();
    try {//from  w  ww. j a  v a2 s . c om
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {
            int totalColumn = cursor.getColumnCount();
            JSONObject rowObject = new JSONObject();
            for (int i = 0; i < totalColumn; i++) {
                if (cursor.getColumnName(i) != null) {
                    try {
                        if (cursor.getString(i) != null)
                            rowObject.put(cursor.getColumnName(i), cursor.getString(i));
                        else
                            rowObject.put(cursor.getColumnName(i), "");
                    } catch (Exception e) {
                    }
                }
            }
            resultSet.put(rowObject);
            cursor.moveToNext();
        }
        cursor.close();
    } finally {
        cursor.close();
    }
    return resultSet;
}

From source file:it.bradipao.berengar.DbTool.java

public static JSONObject table2json(SQLiteDatabase mDB, String sTableName, String sTableSql) {
    // vars//from   w  ww. j ava  2s.  com
    JSONObject jsonTable = new JSONObject();
    JSONArray jsonRows = new JSONArray();
    JSONArray jsonColsName = new JSONArray();
    JSONArray jsonCols = null;

    // read table
    String sqlquery = "select * from " + sTableName;
    Cursor cur = mDB.rawQuery(sqlquery, null);
    // iteratew through rows
    int i = -1;
    while (cur.moveToNext()) {
        // at first element store column names
        if (i == -1)
            for (i = 0; i < cur.getColumnCount(); i++) {
                jsonColsName.put(cur.getColumnName(i));
            }
        // get values
        jsonCols = new JSONArray();
        for (i = 0; i < cur.getColumnCount(); i++) {
            jsonCols.put(cur.getString(i));
        }
        // add values to rows array
        jsonRows.put(jsonCols);
    }

    // final json building
    try {
        // table name
        jsonTable.put("table_name", sTableName);
        // code for create table
        if ((sTableSql != null) && (!sTableSql.isEmpty()))
            jsonTable.put("table_sql", sTableSql);
        // columns name
        jsonTable.put("cols_name", jsonColsName);
        // rows
        jsonTable.put("rows", jsonRows);
    } catch (JSONException e) {
        Log.e(LOGTAG, "error in table2json", e);
    }

    // return String
    return jsonTable;
}

From source file:fr.unix_experience.owncloud_sms.engine.SmsFetcher.java

public JSONArray getLastMessage(MailboxID mbID) {
    String mbURI = mapMailboxIDToURI(mbID);

    if (_context == null || mbURI == null) {
        return null;
    }//  w ww .j  a  v  a 2s. c  o  m

    // Fetch Sent SMS Message from Built-in Content Provider
    Cursor c = (new SmsDataProvider(_context)).query(mbURI);

    c.moveToNext();

    // We create a list of strings to store results
    JSONArray results = new JSONArray();

    JSONObject entry = new JSONObject();

    try {
        for (int idx = 0; idx < c.getColumnCount(); idx++) {
            String colName = c.getColumnName(idx);

            // Id column is must be an integer
            if (colName.equals(new String("_id")) || colName.equals(new String("type"))) {
                entry.put(colName, c.getInt(idx));

                // bufferize Id for future use
                if (colName.equals(new String("_id"))) {
                }
            }
            // Seen and read must be pseudo boolean
            else if (colName.equals(new String("read")) || colName.equals(new String("seen"))) {
                entry.put(colName, c.getInt(idx) > 0 ? "true" : "false");
            } else {
                entry.put(colName, c.getString(idx));
            }
        }

        // Mailbox ID is required by server
        entry.put("mbox", mbID.ordinal());

        results.put(entry);
    } catch (JSONException e) {
        Log.e(TAG, "JSON Exception when reading SMS Mailbox", e);
        c.close();
    }

    c.close();

    return results;
}

From source file:us.dustinj.locationstore.io.LocationExporter.java

private JSONArray getJSON(Cursor queryCursor) {
    JSONArray locations = new JSONArray();

    try {//from w w  w.j  a  va 2  s .  c o  m
        while (!queryCursor.isAfterLast()) {
            JSONObject location = new JSONObject();
            HashMap<String, DatabaseField> databaseFields = LocationDatabase.GetColumnDefinitions();

            for (int columnIndex = 0; columnIndex < queryCursor.getColumnCount(); columnIndex++) {
                DatabaseField currentField = databaseFields.get(queryCursor.getColumnName(columnIndex));

                if (currentField.Type.equals("TEXT")) {
                    location.put(queryCursor.getColumnName(columnIndex), queryCursor.getString(columnIndex));
                } else if (currentField.Type.equals("REAL")) {
                    location.put(queryCursor.getColumnName(columnIndex), queryCursor.getDouble(columnIndex));
                } else if (currentField.Type.equals("INTEGER")) {
                    location.put(queryCursor.getColumnName(columnIndex), queryCursor.getInt(columnIndex));
                } else {
                    Log.e(this.getClass().getSimpleName(), "Read unsupported type from the database");
                }
            }
            queryCursor.moveToNext();

            locations.put(location);
        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return locations;
}

From source file:com.polyvi.xface.extension.XStorageExt.java

/**
 * ?./*from ww w  .  ja va2s  .com*/
 *
 * @param cur
 *            
 * @param tx_id
 *             id
 * @param callbackCtx
 *            
 */
private void processResults(Cursor cur, String tx_id, XCallbackContext callbackCtx) {

    String result = "[]";

    if (cur.moveToFirst()) {
        JSONArray fullresult = new JSONArray();
        String key = "";
        String value = "";
        int colCount = cur.getColumnCount();

        do {
            JSONObject row = new JSONObject();
            try {
                for (int i = 0; i < colCount; ++i) {
                    key = cur.getColumnName(i);
                    value = cur.getString(i);
                    row.put(key, value);
                }
                fullresult.put(row);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } while (cur.moveToNext());

        result = fullresult.toString();
    }

    String jsScript = "xFace.require('xFace/extension/android/storage').completeQuery('" + tx_id + "', "
            + result + ");";
    mWebContext.getApplication().loadJavascript(jsScript);
}

From source file:com.phonegap.Storage.java

/**
 * Process query results.//ww w.j  a  v a 2s .c  o m
 * 
 * @param cur            Cursor into query results
 * @param tx_id            Transaction id
 */
public void processResults(Cursor cur, String tx_id) {

    String result = "[]";
    // If query result has rows

    if (cur.moveToFirst()) {
        JSONArray fullresult = new JSONArray();
        String key = "";
        String value = "";
        int colCount = cur.getColumnCount();

        // Build up JSON result object for each row
        do {
            JSONObject row = new JSONObject();
            try {
                for (int i = 0; i < colCount; ++i) {
                    key = cur.getColumnName(i);
                    value = cur.getString(i);
                    row.put(key, value);
                }
                fullresult.put(row);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } while (cur.moveToNext());

        result = fullresult.toString();
    }

    // Let JavaScript know that there are no more rows
    this.sendJavascript("droiddb.completeQuery('" + tx_id + "', " + result + ");");

}

From source file:fr.unix_experience.owncloud_sms.engine.SmsFetcher.java

public void bufferizeMessagesSinceDate(MailboxID mbID, Long sinceDate) {
    String mbURI = mapMailboxIDToURI(mbID);

    if (_context == null || mbURI == null) {
        return;// w w  w  . ja  v a2s . com
    }

    Cursor c = new SmsDataProvider(_context).query(mbURI, "date > ?", new String[] { sinceDate.toString() });

    // Reading mailbox
    if (c != null && c.getCount() > 0) {
        c.moveToFirst();
        do {
            JSONObject entry = new JSONObject();

            try {
                for (int idx = 0; idx < c.getColumnCount(); idx++) {
                    String colName = c.getColumnName(idx);

                    // Id column is must be an integer
                    if (colName.equals(new String("_id")) || colName.equals(new String("type"))) {
                        entry.put(colName, c.getInt(idx));

                        // bufferize Id for future use
                        if (colName.equals(new String("_id"))) {
                        }
                    }
                    // Seen and read must be pseudo boolean
                    else if (colName.equals(new String("read")) || colName.equals(new String("seen"))) {
                        entry.put(colName, c.getInt(idx) > 0 ? "true" : "false");
                    } else {
                        // Special case for date, we need to record last without searching
                        if (colName.equals(new String("date"))) {
                            final Long tmpDate = c.getLong(idx);
                            if (tmpDate > _lastMsgDate) {
                                _lastMsgDate = tmpDate;
                            }
                        }
                        entry.put(colName, c.getString(idx));
                    }
                }

                // Mailbox ID is required by server
                entry.put("mbox", mbID.ordinal());

                _jsonDataDump.put(entry);

            } catch (JSONException e) {
                Log.e(TAG, "JSON Exception when reading SMS Mailbox", e);
                c.close();
            }
        } while (c.moveToNext());

        Log.d(TAG, c.getCount() + " messages read from " + mbURI);

        c.close();
    }
}

From source file:org.apache.cordova.legacywebsql.LegacyWebSql.java

/**
 * Process query results.//from   w ww  . j a  v a 2s  .c  om
 *
 * @param cur
 *            Cursor into query results
 * @param tx_id
 *            Transaction id
 */
public void processResults(Cursor cur, String tx_id) {

    String result = "[]";
    // If query result has rows

    if (cur.moveToFirst()) {
        JSONArray fullresult = new JSONArray();
        String key = "";
        String value = "";
        int colCount = cur.getColumnCount();

        // Build up JSON result object for each row
        do {
            JSONObject row = new JSONObject();
            try {
                for (int i = 0; i < colCount; ++i) {
                    key = cur.getColumnName(i);
                    value = cur.getString(i);
                    row.put(key, value);
                }
                fullresult.put(row);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } while (cur.moveToNext());

        result = fullresult.toString();
    }

    // Let JavaScript know that there are no more rows
    this.webView.sendJavascript("cordova.require('cordova/plugin/android/storage').completeQuery('" + tx_id
            + "', " + result + ");");
}

From source file:org.skt.runtime.original.Storage.java

/**
 * Process query results./*w ww . ja  v  a  2 s.  c o m*/
 * 
 * @param cur
 *            Cursor into query results
 * @param tx_id
 *            Transaction id
 */
public void processResults(Cursor cur, String tx_id) {

    String result = "[]";
    // If query result has rows

    if (cur.moveToFirst()) {
        JSONArray fullresult = new JSONArray();
        String key = "";
        String value = "";
        int colCount = cur.getColumnCount();

        // Build up JSON result object for each row
        do {
            JSONObject row = new JSONObject();
            try {
                for (int i = 0; i < colCount; ++i) {
                    key = cur.getColumnName(i);
                    value = cur.getString(i);
                    row.put(key, value);
                }
                fullresult.put(row);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } while (cur.moveToNext());

        result = fullresult.toString();
    }

    // Let JavaScript know that there are no more rows
    this.sendJavascript(
            "srt.sktrequire('srt/plugin/android/storage').completeQuery('" + tx_id + "', " + result + ");");
}

From source file:com.phonegap.plugin.sqlitePlugin.SQLitePlugin.java

/**
 * Process query results./*w w w  .ja  va  2 s.  c o  m*/
 *
 * @param cur
 *            Cursor into query results
 * @param tx_id
 *            Transaction id
 */
public void processResults(Cursor cur, String tx_id) {

    String result = "[]";
    // If query result has rows

    if (cur.moveToFirst()) {
        JSONArray fullresult = new JSONArray();
        String key = "";
        String value = "";
        int colCount = cur.getColumnCount();

        // Build up JSON result object for each row
        do {
            JSONObject row = new JSONObject();
            try {
                for (int i = 0; i < colCount; ++i) {
                    key = cur.getColumnName(i);
                    value = cur.getString(i);
                    row.put(key, value);
                }
                fullresult.put(row);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } while (cur.moveToNext());

        result = fullresult.toString();
    }

    // Let JavaScript know that there are no more rows
    this.sendJavascript("dddb.completeQuery('" + tx_id + "', " + result + ");");
}