Is it possible to get column data by column name instead of index in sqlite? I don't want to rely on a specific order of the columns. If so, what's the syntax?
1 Answer
You probably want to use Cursor.getColumnIndex() method.
Returns the zero-based index for the given column name, or -1 if the column doesn't exist. If you expect the column to exist use getColumnIndexOrThrow(String) instead, which will make the error more clear.
Example:
String[] columns = new String[]{ COLUMN_XML };
Cursor c = db.query(TABLE, columns, where, whereArgs, null, null, null);
String xml = c.getString(c.getColumnIndex(COLUMN_XML));
1 Comment
Roy Hinkley
Thanks. Just needed to think about it differently and nest your solution.