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?
To find the column name of the table, you should execute select * from tbl_name and you will get the result in sqlite3_stmt * . and check the column iterate over the total fetched column. Please refer following code for the same. This will print all the column names of the result set.
moveToNext() Move the cursor to the next row.
The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases.
Cursors are what contain the result set of a query made against a database in Android. The Cursor class has an API that allows an app to read (in a type-safe manner) the columns that were returned from the query as well as iterate over the rows of the result set.
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));
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