Android SQLite - Cursor & ContentValues
Asked Answered
A

5

27

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.

Avalos answered 28/10, 2011 at 17:2 Comment(0)
K
61

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();  
Keeton answered 3/1, 2012 at 8:3 Comment(0)
O
3

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;
}
Overseas answered 19/1, 2015 at 6:30 Comment(0)
W
2

You can go to thenewboston there's a tut for SQLite(and using ContentValues) from 111-124 :D

Anyway, the one that he taught about ContentValues is in the 117th

Good Luck :D

PS : But he also use a Cursor :)

Whipstall answered 19/3, 2012 at 8:6 Comment(0)
W
1

No. You have to do that with cursor and old good query. I'd be happy if query could return a array of CV objects.

Wyly answered 28/10, 2011 at 17:8 Comment(0)
C
0

Nope, you have to use the Cursor.

Circumrotate answered 28/10, 2011 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.