Identifying datatype of a column in an SQLite Android Cursor

后端 未结 4 2148
小鲜肉
小鲜肉 2020-12-07 01:12

Is there any way to identify the datatype of a column in a cursor in Android. The cursor object has a number of methods to get the columnname, column value.

I want t

4条回答
  •  既然无缘
    2020-12-07 01:26

    I didn't tested yet but, try to use cursor.getType(i)

    Like this:

    public static List resultSetToArrayListAndroid(Cursor cursor) throws SQLException {
        int columns = cursor.getColumnCount();
        ArrayList list = new ArrayList();
        while (cursor.moveToNext()) {
            HashMap row = new HashMap(columns);
            for (int i = 1; i <= columns; ++i) {
                switch (cursor.getType(i))  {
                    case Cursor.FIELD_TYPE_FLOAT:
                        row.put(cursor.getColumnName(i), cursor.getFloat(i));
                        break;
                    case Cursor.FIELD_TYPE_INTEGER:
                        row.put(cursor.getColumnName(i), cursor.getInt(i));
                        break;
                    case Cursor.FIELD_TYPE_STRING:
                        row.put(cursor.getColumnName(i), cursor.getString(i));
                        break;
                }
            }
            list.add(row);
        }
        return list;
    }
    

提交回复
热议问题