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?
moveToNext move the cursor to the next row. and c.getString(0) will always give you the first column if there is one.
getCount. Returns the numbers of rows in the cursor.
To Search for the values define edit text and implement text watcher in database enter a query as shown below: editText. addTextChangedListener(new TextWatcher(){ Cursor cusror; cursor=db. rawQuery("SELECT * FROM "+ DB_NAME + " WHERE " + DB_NAME.id + " = " + DB_NAME.Id + " AND " + DB_NAME.
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
Cursor column names are case sensitive, make sure you match the case specified in the db or column alias name
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
If you know column index in your select query,
cursor.getString(0);
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