How to get column value from sqlite cursor?
Asked Answered
K

5

20

I am trying to get column value from a cursor, this column is generated at run time by calculations inside the query, I am getting a null value of this column, I am able to get a value of all other columns which exists in SQLite table.

I execute the same query on SQLite editor, it also shows the value of generated column in result set.

Why this giving null value when I am retrieving it from cursor?

Kostman answered 23/2, 2012 at 11:21 Comment(3)
Please provide the code that you use to query and retrieve column values.Strickman
@Chandrapal Yadav: are you sure,you are including that column when you fire query? be sure,you don't miss it!Huba
try to look at this link.. using query method.. #10601170Pneumatology
J
62

Very Simple you can get it by either of following way

String id = cursor.getString( cursor.getColumnIndex("id") ); // id is column name in db

or

String id = cursor.getString( cursor.getColumnIndex(0)); // id is first column in db
Johanna answered 23/2, 2012 at 11:29 Comment(2)
What is Cursor in this example?Molarity
Second example should be String id = cursor.getString(0);Sedgemoor
M
3

Cursor column names are case sensitive, make sure you match the case specified in the db or column alias name

Manchukuo answered 9/7, 2012 at 12:43 Comment(0)
S
2
  1. If you don't know column index in select query the follow this,

    Define constant for all fields of table so it is easy to get field name you don't need to verify its spell

    create Database Costant class with name "DBConstant.java" and define table fields

    public static final String ID = "id";

    Then get value from cursor,

    cursor.getString(cursor.getColumnIndex(DBConstant.ID));

    cursor.getColumnIndex(field name); it returns field column index in you select statement cursor.getString(column index). it returns value which is on that column

  2. If you know column index in your select query,

    cursor.getString(0);

Schistosome answered 15/10, 2015 at 11:40 Comment(0)
D
0

If you want to get string value

String id = cursor.getString( cursor.getColumnIndex("name") ); // id is column name in db
or

String id = cursor.getString( cursor.getColumnIndex(0)); 

If you want to get Integer value

String id = cursor.getInt( cursor.getColumnIndex("id") ); // id is column name in db
or

String id = cursor.getInt( cursor.getColumnIndex(0)); 
Daedal answered 26/3, 2019 at 7:13 Comment(0)
I
0

In Android Studio 3.4.1 (I use Kotlin but it does not make any difference here), I've discovered a very odd behaviour.

For instance:

Cursor c = db.rawQuery("Select IDH, ID, NOME, CONTEUDO from CalcHome 
           Inner Join Calcs using(id)", null)

The column names was registered in a rigid way, independent of case of field names in Select. The array component c.columnNames[0] is Idh not IDH

So

int a = c.getInt(c.getColumnIndex("IDH")) // gives a runtime error.

But

int a = c.getInt(c.getColumnIndex("Idh")) // works!
Irreconcilable answered 22/6, 2019 at 20:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.