Is there any way to GET the ContentValues object from the SQLite?
It's very useful, that we can insert ContentValues in DB, and it should be more useful to get the CV from there.
SQLiteCursor. A Cursor implementation that exposes results from a query on a SQLiteDatabase . This interface provides random read-write access to the result set returned by a database query.
The sqlite3. Cursor class is an instance using which you can invoke methods that execute SQLite statements, fetch data from the result sets of the queries. You can create Cursor object using the cursor() method of the Connection object/class.
Calling moveToFirst() does two things: it allows you to test whether the query returned an empty set (by testing the return value) and it moves the cursor to the first result (when the set is not empty).
This class is used to store a set of values that the ContentResolver can process.
You can use the method cursorRowToContentValues(Cursor cursor, ContentValues values) of the DatabaseUtils class.
example
Cursor c = db.query(tableName, tableColumn, where, whereArgs, groupBy, having, orderBy); ArrayList<ContentValues> retVal = new ArrayList<ContentValues>(); ContentValues map; if(c.moveToFirst()) { do { map = new ContentValues(); DatabaseUtils.cursorRowToContentValues(c, map); retVal.add(map); } while(c.moveToNext()); } c.close();
I wrote my own version of the DatabaseUtils.cursorRowToContentValues
method that David-mu mentioned in order to avoid a bug with parsing booleans. It asks the Cursor
to parse ints and floats based on the types in the SQL database, rather than parsing them when calling the methods in ContentValues
.
public static ContentValues cursorRowToContentValues(Cursor cursor) { ContentValues values = new ContentValues(); String[] columns = cursor.getColumnNames(); int length = columns.length; for (int i = 0; i < length; i++) { switch (cursor.getType(i)) { case Cursor.FIELD_TYPE_NULL: values.putNull(columns[i]); break; case Cursor.FIELD_TYPE_INTEGER: values.put(columns[i], cursor.getLong(i)); break; case Cursor.FIELD_TYPE_FLOAT: values.put(columns[i], cursor.getDouble(i)); break; case Cursor.FIELD_TYPE_STRING: values.put(columns[i], cursor.getString(i)); break; case Cursor.FIELD_TYPE_BLOB: values.put(columns[i], cursor.getBlob(i)); break; } } return values; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With