List of usage examples for android.database Cursor getColumnNames
String[] getColumnNames();
From source file:com.danjarvis.documentcontract.DocumentContract.java
/** * Gets the contract details for the provided content URI. * * @return Contract serialized to a JSONObject *//* w w w . j a v a 2s.c om*/ private void getContract(JSONObject args, CallbackContext callback) { try { Uri uri; Cursor cursor; JSONObject response = new JSONObject(); uri = getUri(args); if (null == uri || !(uri.getScheme().equals(ContentResolver.SCHEME_CONTENT))) { callback.error(INVALID_URI_ERROR); return; } cursor = cordova.getActivity().getContentResolver().query(uri, getColumns(args), null, null, null); if (null != cursor && cursor.moveToFirst()) { for (String col : cursor.getColumnNames()) response.put(col, cursor.getString(cursor.getColumnIndex(col))); } cursor.close(); callback.success(response); } catch (JSONException je) { callback.error(je.getMessage()); } }
From source file:com.nolanlawson.cordova.sqlite.SQLitePlugin.java
private SQLitePLuginResult doSelectInBackgroundAndPossiblyThrow(String sql, String[] bindArgs, SQLiteDatabase db) {/* w w w . j a va2s . c o m*/ debug("\"all\" query: %s", sql); Cursor cursor = null; try { cursor = db.rawQuery(sql, bindArgs); int numRows = cursor.getCount(); if (numRows == 0) { return EMPTY_RESULT; } int numColumns = cursor.getColumnCount(); Object[][] rows = new Object[numRows][]; String[] columnNames = cursor.getColumnNames(); for (int i = 0; cursor.moveToNext(); i++) { Object[] row = new Object[numColumns]; for (int j = 0; j < numColumns; j++) { row[j] = getValueFromCursor(cursor, j, cursor.getType(j)); } rows[i] = row; } debug("returning %d rows", numRows); return new SQLitePLuginResult(rows, columnNames, 0, 0, null); } finally { if (cursor != null) { cursor.close(); } } }
From source file:com.dileepindia.cordova.sms.SMSPlugin.java
private JSONObject getJsonFromCursor(Cursor cur) { JSONObject json = new JSONObject(); int nCol = cur.getColumnCount(); String[] keys = cur.getColumnNames(); try {/* ww w. j av a2 s. co m*/ for (int j = 0; j < nCol; j++) { switch (cur.getType(j)) { /*case 0: json.put(keys[j], null); break; case 1: json.put(keys[j], cur.getInt(j)); break; case 2: json.put(keys[j], cur.getLong(j)); break; case 3: json.put(keys[j], cur.getFloat(j)); break; case 4: json.put(keys[j], cur.getString(j)); break; case 5: json.put(keys[j], cur.getBlob(j)); */ case Cursor.FIELD_TYPE_BLOB: json.put(keys[j], cur.getBlob(j).toString()); break; case Cursor.FIELD_TYPE_FLOAT: json.put(keys[j], cur.getDouble(j)); break; case Cursor.FIELD_TYPE_INTEGER: json.put(keys[j], cur.getLong(j)); break; case Cursor.FIELD_TYPE_NULL: json.put(keys[j], cur); break; case Cursor.FIELD_TYPE_STRING: json.put(keys[j], cur.getString(j)); break; } } } catch (Exception e) { e.printStackTrace(); return null; } return json; }
From source file:it.gmariotti.android.examples.googleaccount.SmsBackupGDriveActivity.java
private JSONObject readSmsInbox() throws JSONException { Cursor cursor = getSmsInboxCursor(); // StringBuilder messages = new StringBuilder(); JSONArray messages = new JSONArray(); String and = ""; if (cursor != null) { final String[] columns = cursor.getColumnNames(); while (cursor.moveToNext()) { // messages.append(and); JSONObject msg = new JSONObject(); // long id = cursor.getLong(SmsQuery._ID); long contactId = cursor.getLong(SmsQuery.PERSON); String address = cursor.getString(SmsQuery.ADDRESS); // long threadId = cursor.getLong(SmsQuery.THREAD_ID); // final long date = cursor.getLong(SmsQuery.DATE); final Map<String, String> msgMap = new HashMap<String, String>(columns.length); for (int i = 0; i < columns.length; i++) { String value = cursor.getString(i); msgMap.put(columns[i], cursor.getString(i)); /*/*from w ww. java 2 s. c o m*/ * messages.append(columns[i]); messages.append("="); * messages.append(value); messages.append(";;"); */ msg.put(columns[i], value); } // and = "\n"; /** * Retrieve display name * */ String displayName = address; if (contactId > 0) { // Retrieve from Contacts with contact id Cursor contactCursor = tryOpenContactsCursorById(contactId); if (contactCursor != null) { if (contactCursor.moveToFirst()) { displayName = contactCursor.getString(RawContactsQuery.DISPLAY_NAME); } else { contactId = 0; } contactCursor.close(); } } else { if (contactId <= 0) { // Retrieve with address Cursor contactCursor = tryOpenContactsCursorByAddress(address); if (contactCursor != null) { if (contactCursor.moveToFirst()) { displayName = contactCursor.getString(ContactsQuery.DISPLAY_NAME); } contactCursor.close(); } } } /* * messages.append("displayName"); messages.append("="); * messages.append(displayName); messages.append(";;"); * messages.append("||"); */ msg.put("displayName", displayName); messages.put(msg); } } JSONObject fileBackupTest = new JSONObject(); fileBackupTest.put("messages", messages); return fileBackupTest; }
From source file:org.disciple.db.Abatis.java
/** * * @param sqlId SQLID/*from w w w.j a va2 s .c o m*/ * @param bindParams sql parameter * @param bean bean class of result * * @return List<Map<String, Object>> result */ @SuppressWarnings({ "unchecked", "rawtypes" }) public <T> List<T> executeForBeanList(String sqlId, Map<String, Object> bindParams, Class bean) { getDbObject(); int pointer = context.getResources().getIdentifier(sqlId, "string", context.getPackageName()); if (pointer == 0) { Log.e(TAG, "undefined sql id"); return null; } String sql = context.getResources().getString(pointer); if (bindParams != null) { Iterator<String> mapIterator = bindParams.keySet().iterator(); while (mapIterator.hasNext()) { String key = mapIterator.next(); Object value = bindParams.get(key); sql = sql.replaceAll("#" + key.toLowerCase() + "#", value == null ? null : "'" + value.toString() + "'"); } } if (sql.indexOf('#') != -1) { Log.e(TAG, "undefined parameter"); return null; } Cursor cursor = dbObj.rawQuery(sql, null); List<T> objectList = new ArrayList<T>(); if (cursor == null) { return null; } String[] columnNames = cursor.getColumnNames(); List<String> dataNames = new ArrayList<String>(); for (String columnName : columnNames) { dataNames.add(chgDataName(columnName)); } T beanObj = null; // get bean class package Package beanPackage = bean.getPackage(); while (cursor.moveToNext()) { Map<String, Object> map = new HashMap<String, Object>(); int i = 0; for (String dataName : dataNames) { map.put(dataName, cursor.getString(i)); i++; } JSONObject json = new JSONObject(map); try { beanObj = (T) parse(json.toString(), bean, beanPackage.getName()); } catch (Exception e) { Log.d(TAG, e.toString()); return null; } objectList.add(beanObj); } cursor.close(); dbObj.close(); return objectList; }
From source file:android.support.content.InMemoryCursor.java
/** * @param cursor source of data to copy. Ownership is reserved to the called, meaning * we won't ever close it. *//*from w w w .ja va 2s .c om*/ InMemoryCursor(Cursor cursor, int offset, int length, int disposition) { checkArgument(offset < cursor.getCount()); // NOTE: The cursor could simply be saved to a field, but we choose to wrap // in a dedicated relay class to avoid hanging directly onto a reference // to the cursor...so future authors are not enticed to think there's // a live link between the delegate cursor and this cursor. mObserverRelay = new ObserverRelay(cursor); mColumnNames = cursor.getColumnNames(); mRowCount = Math.min(length, cursor.getCount() - offset); int numColumns = cursor.getColumnCount(); mExtras = ContentPager.buildExtras(cursor.getExtras(), cursor.getCount(), disposition); mColumnType = new int[numColumns]; mTypedColumnIndex = new int[NUM_TYPES][numColumns]; mColumnTypeCount = new int[NUM_TYPES]; if (!cursor.moveToFirst()) { throw new RuntimeException("Can't position cursor to first row."); } for (int col = 0; col < numColumns; col++) { int type = cursor.getType(col); mColumnType[col] = type; mTypedColumnIndex[type][col] = mColumnTypeCount[type]++; } mLongs = new long[mRowCount * mColumnTypeCount[FIELD_TYPE_INTEGER]]; mDoubles = new double[mRowCount * mColumnTypeCount[FIELD_TYPE_FLOAT]]; mBlobs = new byte[mRowCount * mColumnTypeCount[FIELD_TYPE_BLOB]][]; mStrings = new String[mRowCount * mColumnTypeCount[FIELD_TYPE_STRING]]; for (int row = 0; row < mRowCount; row++) { if (!cursor.moveToPosition(offset + row)) { throw new RuntimeException("Unable to position cursor."); } // Now copy data from the row into primitive arrays. for (int col = 0; col < mColumnType.length; col++) { int type = mColumnType[col]; int position = getCellPosition(row, col, type); switch (type) { case FIELD_TYPE_NULL: throw new UnsupportedOperationException("Not implemented."); case FIELD_TYPE_INTEGER: mLongs[position] = cursor.getLong(col); break; case FIELD_TYPE_FLOAT: mDoubles[position] = cursor.getDouble(col); break; case FIELD_TYPE_BLOB: mBlobs[position] = cursor.getBlob(col); break; case FIELD_TYPE_STRING: mStrings[position] = cursor.getString(col); break; } } } }
From source file:org.disciple.db.Abatis.java
/** * * @param sqlId SQLID//from w w w .jav a2 s. c om * @param bindParams sql parameter * @param bean bean class of result * * @return List<Map<String, Object>> result */ @SuppressWarnings({ "unchecked", "rawtypes" }) public <T> T executeForBean(String sqlId, Map<String, Object> bindParams, Class bean) { getDbObject(); int pointer = context.getResources().getIdentifier(sqlId, "string", context.getPackageName()); if (pointer == 0) { Log.e(TAG, "undefined sql id"); return null; } String sql = context.getResources().getString(pointer); if (bindParams != null) { Iterator<String> mapIterator = bindParams.keySet().iterator(); while (mapIterator.hasNext()) { String key = mapIterator.next(); Object value = bindParams.get(key); sql = sql.replaceAll("#" + key.toLowerCase() + "#", value == null ? null : "'" + value.toString() + "'"); } } if (sql.indexOf('#') != -1) { Log.e(TAG, "undefined parameter"); return null; } Cursor cursor = dbObj.rawQuery(sql, null); List<T> objectList = new ArrayList<T>(); if (cursor == null) { return null; } String[] columnNames = cursor.getColumnNames(); List<String> dataNames = new ArrayList<String>(); for (String columnName : columnNames) { dataNames.add(chgDataName(columnName)); } T beanObj = null; // get bean class package Package beanPackage = bean.getPackage(); while (cursor.moveToNext()) { Map<String, Object> map = new HashMap<String, Object>(); int i = 0; for (String dataName : dataNames) { map.put(dataName, cursor.getString(i)); i++; } JSONObject json = new JSONObject(map); try { beanObj = (T) parse(json.toString(), bean, beanPackage.getName()); } catch (Exception e) { Log.d(TAG, e.toString()); return null; } objectList.add(beanObj); } if (objectList.size() <= 0) { return null; } cursor.close(); dbObj.close(); return objectList.get(0); }
From source file:org.disciple.db.Abatis.java
/** * * @param sqlId SQLID/*from ww w. j av a2s. c om*/ * @param bindParams sql parameter * * @return List<Map<String, Object>> result */ public List<Map<String, Object>> executeForMapList(String sqlId, Map<String, Object> bindParams) { getDbObject(); int pointer = context.getResources().getIdentifier(sqlId, "string", context.getPackageName()); if (pointer == 0) { Log.e(TAG, "undefined sql id"); return null; } String sql = context.getResources().getString(pointer); if (bindParams != null) { Iterator<String> mapIterator = bindParams.keySet().iterator(); while (mapIterator.hasNext()) { String key = mapIterator.next(); Object value = bindParams.get(key); sql = sql.replaceAll("#" + key.toLowerCase() + "#", value == null ? null : "'" + value.toString() + "'"); } } if (sql.indexOf('#') != -1) { Log.e(TAG, "undefined parameter"); return null; } Cursor cursor = dbObj.rawQuery(sql, null); List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(); if (cursor == null) { return null; } String[] columnNames = cursor.getColumnNames(); while (cursor.moveToNext()) { Map<String, Object> map = new HashMap<String, Object>(); int i = 0; for (String columnName : columnNames) { map.put(columnName, cursor.getString(i)); i++; } mapList.add(map); } cursor.close(); dbObj.close(); return mapList; }
From source file:org.disciple.db.Abatis.java
/** * @param sqlId SQLID//from www .j a v a 2 s. co m * @param bindParams sql parameter * * @return Map<String, Object> result */ public Map<String, Object> executeForMap(String sqlId, Map<String, Object> bindParams) { getDbObject(); int pointer = context.getResources().getIdentifier(sqlId, "string", context.getPackageName()); if (pointer == 0) { Log.e(TAG, "undefined sql id"); return null; } String sql = context.getResources().getString(pointer); if (bindParams != null) { Iterator<String> mapIterator = bindParams.keySet().iterator(); while (mapIterator.hasNext()) { String key = mapIterator.next(); Object value = bindParams.get(key); sql = sql.replaceAll("#" + key.toLowerCase() + "#", value == null ? null : "'" + value.toString() + "'"); } } if (sql.indexOf('#') != -1) { Log.e(TAG, "undefined parameter"); return null; } Cursor cursor = dbObj.rawQuery(sql, null); List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(); if (cursor == null) { return null; } String[] columnNames = cursor.getColumnNames(); while (cursor.moveToNext()) { Map<String, Object> map = new HashMap<String, Object>(); int i = 0; for (String columnName : columnNames) { map.put(columnName, cursor.getString(i)); i++; } mapList.add(map); } if (mapList.size() <= 0) { return null; } cursor.close(); dbObj.close(); return mapList.get(0); }
From source file:com.zegoggles.smssync.CursorToMessage.java
public ConversionResult cursorToMessages(final Cursor cursor, final int maxEntries, DataType dataType) throws MessagingException { final String[] columns = cursor.getColumnNames(); final ConversionResult result = new ConversionResult(dataType); do {//from www. j a v a 2 s . com final long date = cursor.getLong(cursor.getColumnIndex(SmsConsts.DATE)); if (date > result.maxDate) { result.maxDate = date; } final Map<String, String> msgMap = new HashMap<String, String>(columns.length); for (int i = 0; i < columns.length; i++) { msgMap.put(columns[i], cursor.getString(i)); } Message m = null; switch (dataType) { case SMS: m = messageFromMapSms(msgMap); break; case MMS: m = messageFromMapMms(msgMap); break; case CALLLOG: m = messageFromMapCallLog(msgMap); break; } if (m != null) { result.messageList.add(m); result.mapList.add(msgMap); } } while (result.messageList.size() < maxEntries && cursor.moveToNext()); return result; }