Java Code Examples for android.database.Cursor.getColumnName()

The following are Jave code examples for showing how to use getColumnName() of the android.database.Cursor class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: FuelUp   File: JsonUtil.java   Source Code and License Vote up 6 votes
private static JSONObject getVehicleAsJson(final Long id, Context context) throws JSONException {
    String[] selectionArgs = { String.valueOf(id) };
    Cursor cursor = context.getContentResolver().query(VehicleEntry.CONTENT_URI,
            FuelUpContract.ALL_COLUMNS_VEHICLES, VehicleEntry._ID + "=?",
            selectionArgs, null);

    JSONObject row = new JSONObject();
    if (cursor != null) {
        cursor.moveToFirst();
        for (int i = 0; i < cursor.getColumnCount(); i++) {
            if (cursor.getColumnName(i) != null
                    && !cursor.getColumnName(i).equals(VehicleEntry._ID)
                    && !cursor.getColumnName(i).equals(VehicleEntry.COLUMN_PICTURE)) {
                row.put(cursor.getColumnName(i), cursor.getString(i));
            }
        }
        cursor.close();
    }

    return row;
}
 
Example 2
Project: FuelUp   File: JsonUtil.java   Source Code and License Vote up 6 votes
private static JSONArray getJsonArrayFromCursor(final Cursor cursor) {
    JSONArray resultSet = new JSONArray();
    if (cursor != null) {
        while (cursor.moveToNext()) {
            JSONObject row = new JSONObject();
            for (int i = 0; i < cursor.getColumnCount(); i++) {
                if (cursor.getColumnName(i) != null
                        && !cursor.getColumnName(i).equals(ExpenseEntry.COLUMN_VEHICLE)
                        && !cursor.getColumnName(i).equals(FillUpEntry.COLUMN_VEHICLE)
                        && !cursor.getColumnName(i).equals(BaseColumns._ID)) {
                    try {
                        row.put(cursor.getColumnName(i), cursor.getString(i));
                    } catch (Exception e) {
                        Log.d(LOG_TAG, e.getMessage());
                    }
                }
            }
            resultSet.put(row);
        }
        cursor.close();
    }

    return resultSet;
}
 
Example 3
Project: GitHub   File: CursorUtils.java   Source Code and License Vote up 5 votes
public static <T> T getEntity(TableEntity<T> table, final Cursor cursor) throws Throwable {
    T entity = table.createEntity();
    HashMap<String, ColumnEntity> columnMap = table.getColumnMap();
    int columnCount = cursor.getColumnCount();
    for (int i = 0; i < columnCount; i++) {
        String columnName = cursor.getColumnName(i);
        ColumnEntity column = columnMap.get(columnName);
        if (column != null) {
            column.setValueFromCursor(entity, cursor, i);
        }
    }
    return entity;
}
 
Example 4
Project: mobile-store   File: InstalledApp.java   Source Code and License Vote up 5 votes
public InstalledApp(Cursor cursor) {

        checkCursorPosition(cursor);

        for (int i = 0; i < cursor.getColumnCount(); i++) {
            String n = cursor.getColumnName(i);
            switch (n) {
                case Schema.InstalledAppTable.Cols._ID:
                    id = cursor.getLong(i);
                    break;
                case Schema.InstalledAppTable.Cols.Package.NAME:
                    packageName = cursor.getString(i);
                    break;
                case Schema.InstalledAppTable.Cols.VERSION_CODE:
                    versionCode = cursor.getInt(i);
                    break;
                case Schema.InstalledAppTable.Cols.VERSION_NAME:
                    versionName = cursor.getString(i);
                    break;
                case Schema.InstalledAppTable.Cols.APPLICATION_LABEL:
                    applicationLabel = cursor.getString(i);
                    break;
                case Schema.InstalledAppTable.Cols.SIGNATURE:
                    signature = cursor.getString(i);
                    break;
                case Schema.InstalledAppTable.Cols.LAST_UPDATE_TIME:
                    lastUpdateTime = cursor.getLong(i);
                    break;
                case Schema.InstalledAppTable.Cols.HASH_TYPE:
                    hashType = cursor.getString(i);
                    break;
                case Schema.InstalledAppTable.Cols.HASH:
                    hash = cursor.getString(i);
                    break;
            }
        }
    }
 
Example 5
Project: CP-Tester   File: ContentProviderHelper.java   Source Code and License Vote up 5 votes
public static String getSQLResult(Context context, ProviderInfo provider, String query) {
    Bundle bundle = new Bundle();
    bundle.putString("provider_name", provider.packageName);
    bundle.putString("query", query);
    FirebaseAnalytics.getInstance(context).logEvent("sql_injection", bundle);
    StringBuilder s = new StringBuilder();
    try {
        Uri uri = Uri.parse("content://" + provider.authority);
        Cursor c = context.getContentResolver().query(uri, new String[]{ query }, null, null, null);
        int col_c = c != null ? c.getColumnCount() : 0;
        String[] Columns = new String[col_c];
        for (int i = 0; i < col_c; i++) {
            s.append(c.getColumnName(i));
            s.append(":");
            Columns[i] = c.getColumnName(i);
        }
        s.append("\n");
        if (c != null && c.moveToFirst()) {
            do {
                for (int i = 0; i < col_c; i++) {
                    if (Columns[i].toLowerCase().contains("image")) {
                        byte[] blob = c.getBlob(i);
                        s.append(bytesToHexString(blob));
                    } else {
                        s.append(c.getString(i));
                    }
                    s.append(";");
                }
                s.append("\n");
            } while (c.moveToNext());
        }
        if (c != null) {
            c.close();
        }
    } catch (Throwable e) {
        s.append(e.getMessage());
    }
    return s.toString();
}
 
Example 6
Project: BlackList   File: ContactsAccessHelper.java   Source Code and License Vote up 5 votes
private static void debug(Cursor cursor) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < cursor.getColumnCount(); i++) {
        String s = cursor.getString(i);
        String n = cursor.getColumnName(i);
        sb.append("[").append(n).append("]=").append(s);
    }
    Log.d(TAG, sb.toString());
}
 
Example 7
Project: RetrofitAppArchitecture   File: Utils.java   Source Code and License Vote up 5 votes
public static Map<String, Object> cursor2Map(Cursor cursor) {
    Map<String, Object> map = new HashMap<>();
    if (cursor != null && cursor.getColumnCount() > 0) {
        int count = cursor.getColumnCount();
        for (int i = 0; i < count; i++) {
            int type = cursor.getType(i);

            Object value = null;
            switch (type) {
                case Cursor.FIELD_TYPE_STRING:
                    value = cursor.getString(i);
                    break;
                case Cursor.FIELD_TYPE_INTEGER:
                    value = cursor.getInt(i);
                    break;
                case Cursor.FIELD_TYPE_FLOAT:
                    value = cursor.getFloat(i);
                    break;
                case Cursor.FIELD_TYPE_NULL:
                    LogUtils.d("未知的参数类型,type:" + type);
                    break;
                case Cursor.FIELD_TYPE_BLOB:
                    LogUtils.d("未知的参数类型,type:" + type);
                    break;
            }
            if (value != null) {
                String name = cursor.getColumnName(i);
                map.put(name, value);
            }
        }
    }
    return map;
}
 
Example 8
Project: HueNotifier   File: Logger.java   Source Code and License Vote up 5 votes
public static void log(final Cursor c) {
    if (!BuildConfig.DEBUG) return;
    c.moveToFirst();
    String title = "";
    for (int i = 0; i < c.getColumnCount(); i++)
        title += c.getColumnName(i) + " | ";
    log(title);
    while (!c.isAfterLast()) {
        title = "";
        for (int i = 0; i < c.getColumnCount(); i++)
            title += c.getString(i) + " | ";
        log(title);
        c.moveToNext();
    }
}
 
Example 9
Project: RLibrary   File: ContactsPickerHelper.java   Source Code and License Vote up 5 votes
private static void log(final Cursor cursor) {
    if (cursor != null) {
        if (cursor.getCount() > 0) {
            L.e("----------------------start--------------------");
            L.i("数量:" + cursor.getCount() + " 列数:" + cursor.getColumnCount());
            if (cursor.moveToFirst()) {
                do {
                    for (int i = 0; i < cursor.getColumnCount(); i++) {
                        final String columnName = cursor.getColumnName(i);
                        final int columnIndex = cursor.getColumnIndex(columnName);
                        final int type = cursor.getType(columnIndex);
                        String data = "", ty = "";
                        if (type == Cursor.FIELD_TYPE_NULL) {
                            ty = "NULL";
                            data = "空值";
                        } else if (type == Cursor.FIELD_TYPE_BLOB) {
                            ty = "BLOB";
                            final byte[] bytes = cursor.getBlob(columnIndex);
                            data = String.valueOf(bytes) + " " + bytes.length * 1f / 1024f + "KB";
                        } else if (type == Cursor.FIELD_TYPE_FLOAT) {
                            ty = "FLOAT";
                            data = String.valueOf(cursor.getFloat(columnIndex));
                        } else if (type == Cursor.FIELD_TYPE_INTEGER) {
                            ty = "INTEGER";
                            data = String.valueOf(cursor.getInt(columnIndex));
                        } else if (type == FIELD_TYPE_STRING) {
                            ty = "STRING";
                            data = cursor.getString(columnIndex);
                        }

                        L.i("第" + i + "列->名称:" + columnName + " 索引:" + columnIndex + " 类型:" + ty + " 值:" + data);
                    }
                } while (cursor.moveToNext());
            }
            L.e("------------------------end---------------------");
        }
        cursor.close();
    }
}
 
Example 10
Project: Pluto-Android   File: CursorUtils.java   Source Code and License Vote up 4 votes
public static <T> T getEntity(Cursor cursor, Class<T> clazz,FinalDb db){
	try {
		if(cursor!=null ){
			TableInfo table = TableInfo.get(clazz);
			int columnCount = cursor.getColumnCount();
			if(columnCount>0){
				T  entity = (T) clazz.newInstance();
				for(int i=0;i<columnCount;i++){
					
					String column = cursor.getColumnName(i);
					
					Property property = table.propertyMap.get(column);
					if(property!=null){
						property.setValue(entity, cursor.getString(i));
					}else{
						if(table.getId().getColumn().equals(column)){
							table.getId().setValue(entity,  cursor.getString(i));
						}
					}

				}
                   /**
                    * 处理OneToMany的lazyLoad形式
                    */
                   for(OneToMany oneToManyProp : table.oneToManyMap.values()){
                       if(oneToManyProp.getDataType()==OneToManyLazyLoader.class){
                           OneToManyLazyLoader oneToManyLazyLoader = new OneToManyLazyLoader(entity,clazz,oneToManyProp.getOneClass(),db);
                           oneToManyProp.setValue(entity,oneToManyLazyLoader);
                       }
                   }

                   /**
                    * 处理ManyToOne的lazyLoad形式
                    */
                   for(ManyToOne manyToOneProp : table.manyToOneMap.values()){
                       if(manyToOneProp.getDataType()==ManyToOneLazyLoader.class){
                           ManyToOneLazyLoader manyToOneLazyLoader = new ManyToOneLazyLoader(entity,clazz,manyToOneProp.getManyClass(),db);
                           manyToOneLazyLoader.setFieldValue(cursor.getInt(cursor.getColumnIndex(manyToOneProp.getColumn())));
                           manyToOneProp.setValue(entity,manyToOneLazyLoader);
                       }
                   }
				return entity;
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	return null;
}
 
Example 11
Project: mobile-store   File: Repo.java   Source Code and License Vote up 4 votes
public Repo(Cursor cursor) {

        checkCursorPosition(cursor);

        for (int i = 0; i < cursor.getColumnCount(); i++) {
            switch (cursor.getColumnName(i)) {
                case Cols._ID:
                    id = cursor.getInt(i);
                    break;
                case Cols.LAST_ETAG:
                    lastetag = cursor.getString(i);
                    break;
                case Cols.ADDRESS:
                    address = cursor.getString(i);
                    break;
                case Cols.DESCRIPTION:
                    description = cursor.getString(i);
                    break;
                case Cols.FINGERPRINT:
                    fingerprint = cursor.getString(i);
                    break;
                case Cols.IN_USE:
                    inuse = cursor.getInt(i) == 1;
                    break;
                case Cols.LAST_UPDATED:
                    lastUpdated = Utils.parseTime(cursor.getString(i), null);
                    break;
                case Cols.MAX_AGE:
                    maxage = cursor.getInt(i);
                    break;
                case Cols.VERSION:
                    version = cursor.getInt(i);
                    break;
                case Cols.NAME:
                    name = cursor.getString(i);
                    break;
                case Cols.SIGNING_CERT:
                    signingCertificate = cursor.getString(i);
                    break;
                case Cols.PRIORITY:
                    priority = cursor.getInt(i);
                    break;
                case Cols.IS_SWAP:
                    isSwap = cursor.getInt(i) == 1;
                    break;
                case Cols.USERNAME:
                    username = cursor.getString(i);
                    break;
                case Cols.PASSWORD:
                    password = cursor.getString(i);
                    break;
                case Cols.TIMESTAMP:
                    timestamp = cursor.getLong(i);
                    break;
                case Cols.ICON:
                    icon = cursor.getString(i);
                    break;
                case Cols.MIRRORS:
                    mirrors = Utils.parseCommaSeparatedString(cursor.getString(i));
                    break;
                case Cols.PUSH_REQUESTS:
                    pushRequests = cursor.getInt(i);
                    break;
            }
        }
    }
 
Example 12
Project: Taxi-Datalogger   File: CloudPublisherService.java   Source Code and License Vote up 4 votes
@WorkerThread
void processCollectedSensorData() throws JSONException {
    if (mPublisher == null || !mPublisher.isReady()) {
        return;
    }

    // Query the data we wish to push to the cloud
    long lastSyndId = DatalogDbHelper.getLastSyncId(this);
    Cursor cursor = mDatabase.query(DatalogDbHelper.TABLE_NAME,
            null,
            DatalogDbHelper.DatalogEntry._ID + " > " + lastSyndId,
            null,
            null,
            null,
            BaseColumns._ID + " ASC");
    Log.i(TAG, "publishing " + cursor.getCount() + " log entries");

    final int columnCount = cursor.getColumnCount();
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        JSONObject entryObject = new JSONObject();
        // Add the device ID to the JSON record.
        //
        // FIXME: There should be a way to get Google Cloud IOT to do this on its
        // end, so I don't need to trust the IDs.
        entryObject.put("deviceId", mDeviceId);
        long id = 0;
        for (int i = 0; i < columnCount; i++) {
            String columnName = cursor.getColumnName(i);
            if (columnName != null) {
                // Note the most recent ID we've transmitted, but don't include
                // the ID column in the JSON message
                if (columnName.equals(BaseColumns._ID)) {
                    id = cursor.getLong(i);
                    continue;
                }
                if (cursor.getType(i) == Cursor.FIELD_TYPE_FLOAT) {
                    entryObject.put(columnName, cursor.getDouble(i));
                } else {
                    entryObject.put(columnName, cursor.getString(i));
                }
            }
        }
        // Push message to the cloud
        mPublisher.publish(entryObject.toString());
        Log.d(TAG, "Sent entry: " + (cursor.getPosition()+1) + "/" +
                cursor.getCount());
        if (id > lastSyndId)
            DatalogDbHelper.setLastSyncId(this, id);
    }
    cursor.close();
}
 
Example 13
Project: Android_RuuvitagScannner   File: ScannerService.java   Source Code and License Vote up 4 votes
private void exportDB() {
    File exportDir = new File(Environment.getExternalStorageDirectory(), "ruuvitaglogs");
    String time = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
    if (!exportDir.exists()) {
        if (!exportDir.mkdirs()) {
            Log.e("ScannerService", "failed to create directory");
        }
    }

    try {
        Cursor curCSV = db.rawQuery("SELECT * FROM ruuvitag", null);

        String[] columnNames = {
                curCSV.getColumnName(1),
                curCSV.getColumnName(7),
                curCSV.getColumnName(3),
                curCSV.getColumnName(4),
                curCSV.getColumnName(5),
                curCSV.getColumnName(6),
                curCSV.getColumnName(8)
        };

        while (curCSV.moveToNext()) {
            File file = new File(exportDir, curCSV.getString(1)+"-"+time+".csv");
            FileWriter fw = new FileWriter(file, file.exists());

            CSVWriter writer = new CSVWriter(fw);

            if(file.length() <= 0) {
                writer.writeNext(columnNames);
            }

            String[] arrStr = {
                    curCSV.getString(1),
                    curCSV.getString(7),
                    curCSV.getString(3),
                    curCSV.getString(4),
                    curCSV.getString(5),
                    curCSV.getString(6),
                    curCSV.getString(9).substring(12, 20)
            };

            writer.writeNext(arrStr);
            writer.close();
            fw.close();
        }
        curCSV.close();

    } catch (Exception sqlEx) {
        Log.e("ScannerService", sqlEx.getMessage(), sqlEx);
    }
}