Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android how to get sqlite data by column name

Tags:

android

sqlite

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?

like image 477
Roy Hinkley Avatar asked Nov 08 '12 22:11

Roy Hinkley


People also ask

How to select column names in SQLite?

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.

What is cursor moveToNext()?

moveToNext() Move the cursor to the next row.

Where does SQLite store data in Android?

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.

What is a cursor Android?

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.


1 Answers

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));
like image 92
user802421 Avatar answered Sep 28 '22 10:09

user802421