Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite - Cursor & ContentValues

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.

like image 453
artem Avatar asked Oct 28 '11 17:10

artem


People also ask

What is SQLite cursor Android?

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.

Does SQLite support cursor?

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.

What does cursor moveToFirst do?

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).

What is the use of Contentvalues in Android?

This class is used to store a set of values that the ContentResolver can process.


2 Answers

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();   
like image 110
David-mu Avatar answered Sep 28 '22 17:09

David-mu


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; } 
like image 44
gengkev Avatar answered Sep 28 '22 17:09

gengkev