List of usage examples for android.database Cursor getType
int getType(int columnIndex);
From source file:Main.java
/** * Get the cursor//from ww w . j av a 2 s . c o m * @param cursor Cursor from where read value * @param index int to read * @return Object read */ private static Object getCursorValue(Cursor cursor, int index) { switch (cursor.getType(index)) { case Cursor.FIELD_TYPE_INTEGER: return Integer.valueOf(cursor.getInt(index)); case Cursor.FIELD_TYPE_FLOAT: return Float.valueOf(cursor.getFloat(index)); case Cursor.FIELD_TYPE_STRING: return cursor.getString(index); case Cursor.FIELD_TYPE_BLOB: return cursor.getBlob(index); default: return null; } }
From source file:Main.java
/** * Retrieve the data type of the [i] field in the Cursor. * /*from www.j a v a2 s. co m*/ * @param c * @param i * @return */ public static final Class<?> getIndexDataType(Cursor c, int i) { switch (c.getType(i)) { case Cursor.FIELD_TYPE_STRING: return String.class; case Cursor.FIELD_TYPE_FLOAT: return Double.class; case Cursor.FIELD_TYPE_INTEGER: return Long.class; case Cursor.FIELD_TYPE_NULL: return String.class; default: case Cursor.FIELD_TYPE_BLOB: throw new IllegalStateException("Unexpected data type in SQLite table"); } }
From source file:Main.java
public static Object cursorValue(String column, Cursor cr) { Object value = false;/*from ww w . j a va 2s .c o m*/ int index = cr.getColumnIndex(column); switch (cr.getType(index)) { case Cursor.FIELD_TYPE_NULL: value = false; break; case Cursor.FIELD_TYPE_STRING: value = cr.getString(index); break; case Cursor.FIELD_TYPE_INTEGER: value = cr.getInt(index); break; case Cursor.FIELD_TYPE_FLOAT: value = cr.getFloat(index); break; case Cursor.FIELD_TYPE_BLOB: value = cr.getBlob(index); break; } return value; }
From source file:Main.java
private static int getType(Cursor cursor, int columnIndex) throws Exception { if (Build.VERSION.SDK_INT >= 11) { return cursor.getType(columnIndex); }/*from w w w .j a v a 2s.co m*/ SQLiteCursor sqLiteCursor = (SQLiteCursor) cursor; CursorWindow cursorWindow = sqLiteCursor.getWindow(); int pos = cursor.getPosition(); int type = -1; if (cursorWindow.isNull(pos, columnIndex)) { type = Cursor.FIELD_TYPE_NULL; } else if (cursorWindow.isLong(pos, columnIndex)) { type = Cursor.FIELD_TYPE_INTEGER; } else if (cursorWindow.isFloat(pos, columnIndex)) { type = Cursor.FIELD_TYPE_FLOAT; } else if (cursorWindow.isString(pos, columnIndex)) { type = Cursor.FIELD_TYPE_STRING; } else if (cursorWindow.isBlob(pos, columnIndex)) { type = Cursor.FIELD_TYPE_BLOB; } return type; }
From source file:Main.java
private static String dumpRow(Cursor c) { String result = "VALUES("; for (int pos = 0; pos < c.getColumnCount(); pos++) { switch (c.getType(pos)) { case Cursor.FIELD_TYPE_NULL: result += "null"; break; case Cursor.FIELD_TYPE_INTEGER: result += Integer.toString(c.getInt(pos)); break; case Cursor.FIELD_TYPE_FLOAT: result += Float.toString(c.getFloat(pos)); break; case Cursor.FIELD_TYPE_STRING: result += DatabaseUtils.sqlEscapeString(c.getString(pos)); break; case Cursor.FIELD_TYPE_BLOB: result += "X'"; for (byte b : c.getBlob(pos)) { result += String.format("%02X", b); }/*from w w w .j a v a 2 s .c o m*/ result += "'"; break; default: return "Invalid field type " + c.getType(pos) + " at position " + pos; } if (pos < c.getColumnCount() - 1) { result += ", "; } } return result + ")"; }
From source file:Main.java
public static String logCursor(String prefix, Cursor cr) { StringBuilder sb = new StringBuilder().append(prefix + ": "); for (int i = 0; i < cr.getColumnCount(); i++) { sb.append(cr.getColumnName(i)).append("="); switch (cr.getType(i)) { case Cursor.FIELD_TYPE_NULL: sb.append("NULL"); break; case Cursor.FIELD_TYPE_STRING: sb.append("\"").append(cr.getString(i)).append("\""); break; case Cursor.FIELD_TYPE_INTEGER: sb.append(cr.getInt(i));//from w ww .j a va 2 s.c o m break; case Cursor.FIELD_TYPE_FLOAT: sb.append(cr.getFloat(i)); break; case Cursor.FIELD_TYPE_BLOB: sb.append("BIN(").append(cr.getBlob(i).length).append("b)"); break; } sb.append(" "); } return sb.toString(); }
From source file:Main.java
/** * Return the data stored in the cursor at the given index and given position * (ie the given row which the cursor is currently on) as null OR a String. * <p>/*from w ww .j av a 2 s.c o m*/ * NB: Currently only checks for Strings, long, int, and double. * * @param c * @param i * @return */ @SuppressLint("NewApi") public static String getIndexAsString(Cursor c, int i) { // If you add additional return types here be sure to modify the javadoc. if (i == -1) return null; if (c.isNull(i)) { return null; } switch (c.getType(i)) { case Cursor.FIELD_TYPE_STRING: return c.getString(i); case Cursor.FIELD_TYPE_FLOAT: { // the static version of this seems to have problems... Double d = c.getDouble(i); String v = d.toString(); return v; } case Cursor.FIELD_TYPE_INTEGER: { // the static version of this seems to have problems... Long l = c.getLong(i); String v = l.toString(); return v; } case Cursor.FIELD_TYPE_NULL: return c.getString(i); default: case Cursor.FIELD_TYPE_BLOB: throw new IllegalStateException("Unexpected data type in SQLite table"); } }
From source file:com.taobao.weex.devtools.inspector.protocol.module.Database.java
/** * Flatten all columns and all rows of a cursor to a single array. The array cannot be * interpreted meaningfully without the number of columns. * * @param cursor/*from w w w. j a v a2 s . c om*/ * @param limit Maximum number of rows to process. * @return List of Java primitives matching the value type of each column, converted to * strings. */ private static ArrayList<String> flattenRows(Cursor cursor, int limit) { Util.throwIfNot(limit >= 0); ArrayList<String> flatList = new ArrayList<>(); final int numColumns = cursor.getColumnCount(); for (int row = 0; row < limit && cursor.moveToNext(); row++) { for (int column = 0; column < numColumns; column++) { switch (cursor.getType(column)) { case Cursor.FIELD_TYPE_NULL: flatList.add(null); break; case Cursor.FIELD_TYPE_INTEGER: flatList.add(String.valueOf(cursor.getLong(column))); break; case Cursor.FIELD_TYPE_FLOAT: flatList.add(String.valueOf(cursor.getDouble(column))); break; case Cursor.FIELD_TYPE_BLOB: flatList.add(blobToString(cursor.getBlob(column))); break; case Cursor.FIELD_TYPE_STRING: default: flatList.add(cursor.getString(column)); break; } } } if (!cursor.isAfterLast()) { for (int column = 0; column < numColumns; column++) { flatList.add("{truncated}"); } } return flatList; }
From source file:li.barter.utils.Utils.java
/** * Converts a cursor to a bundle. Field datatypes will be maintained. Floats will be stored in * the Bundle as Doubles, and Integers will be stored as Longs due to Cursor limitationcs * * @param cursor The cursor to convert to a Bundle. This must be positioned to the row to be * read//from w w w .jav a 2s. co m * @return The converted bundle */ public static Bundle cursorToBundle(Cursor cursor) { final int columnCount = cursor.getColumnCount(); final Bundle bundle = new Bundle(columnCount); for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { final String columnName = cursor.getColumnName(columnIndex); switch (cursor.getType(columnIndex)) { case Cursor.FIELD_TYPE_STRING: { bundle.putString(columnName, cursor.getString(columnIndex)); break; } case Cursor.FIELD_TYPE_BLOB: { bundle.putByteArray(columnName, cursor.getBlob(columnIndex)); break; } case Cursor.FIELD_TYPE_FLOAT: { bundle.putDouble(columnName, cursor.getDouble(columnIndex)); break; } case Cursor.FIELD_TYPE_INTEGER: { bundle.putLong(columnName, cursor.getLong(columnIndex)); break; } } } return bundle; }
From source file:org.andstatus.app.util.TypedCursorValue.java
public TypedCursorValue(Cursor cursor, int columnIndex) { type = CursorFieldType.fromColumnType(cursor.getType(columnIndex)); value = type.columnToObject(cursor, columnIndex); }