CursorIndexOutOfBoundsException on SQLite get - android

I want to get data from an SQLite table and I know that table will always have only one record. I'm trying to do that get using:
public User_Token getUser_TokenDB() {
String sql = "SELECT * FROM " + TABLE_NAME_USER_TOKEN +" WHERE ID = " + 1;
Cursor cursor = this.database.rawQuery(sql, null);
User_Token auxUserToken = new User_Token(
cursor.getLong(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3));
return auxUserToken;
}
But I always get:
'java.lang.RuntimeException: Unable to start activity ComponentInfo{com.support.android.iplfit/com.support.android.iplfit.Activities.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0'.
The only way I can access this information is by returning an array of Tokens and do .get(0) but I feel like it is not the right way, since it has no sense to return an array of only one object.

The cursor index starts out of bounds. You need to use Cursor#moveToFirst() which moves the cursor to the 0 index and returns true if there are items in the cursor.
So something like this:
if (cursor.moveToFirst()) {
// Cursor has items and is ready for extraction
} else {
// Cursor has no items.
}

You need to move cursor at first row before you can get data from it. Call moveToNext method before accessing data from cursor
while(cursor.MoveToNext()) {
User_Token auxUserToken = new User_Token(
cursor.getLong(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3));
}

You aren't moving to a position within the cursor, thus the location is before any rows i.e. -1.
You need to move to a row and in your case you want the first row (not that there are any (i.e. the message says size of 0)) and only if the move can be made do you want to extract data. Otherwise you would handle no data available.
The Cursor move???? methods (moveToFirst, moveToLast, moveToNext, moveToPrevious, moveToPosition) all return true if the move can be made, otherwise false.
So your code code be :-
Cursor cursor = this.database.rawQuery(sql, null);
if (cursor.moveToFirst) {
User_Token auxUserToken = new User_Token(
cursor.getLong(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3));
} else {
return null; //???? handle no data how you want perhaps null.
}
return auxUserToken;
As a note it's inadvisable to generally use column offsets, rather the more common way is to get the offset according to the column name using getColumnIndex(column_name). So (assuming the column name is id) would be to replace cursor.getLong(0) with cursor.getLong(cursor.getColumnIndex("id")

Related

displaying the value of a particular row

I am trying to display value of a particular row in android to check if a particular updation. Can I made it successful or not? But as the return type is cursor I don't know how to fetch it. So pls help me.
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from calls where id=" + id + "", null);
return res;
}
Try this...
ArrayList<HashMap<String, String>> dataList = new ArrayList<>();
call your method to get data as arraylist of hashmap
dataList.addAll(loginDataBaseAdapter.getData(id));
or
dataList = loginDataBaseAdapter.getData(id);
in db_class
public ArrayList<HashMap<String, String>> getData(String id) {
ArrayList<HashMap<String, String>> returnArray = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from calls where id=" + id + "", null);
if (res != null && res.getCount() > 0) {
if (res.moveToLast()) {
do {
HashMap<String, String> getdatamap = new HashMap<String, String>();
getdatamap.put("KEY_ONE", res.getString(1));
getdatamap.put("KEY_TWO, res.getString(2));
returnArray.add(getdatamap);
}while (res.moveToPrevious());
}
}
res.close();
return returnArray;
}
Now you can use the values in arraylist to make your view
You can follow the below code:
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from calls where id=" + id + "", null);
if (res != null) {
res.moveToFirst();
}
String firstColumnValue = res.getString(0);
String secondColumnValue = res.getString(1);
And close the database after retrieving values:
db.close();
If you want to get column value by its name, then use the following code:
String value = res.getColumnIndex("column_name");
Get row value as a String. Then return string value.
public String getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from calls where id=" + id + "", null);
if (res.getCount() > 0) {
res.moveToFirst();
String s = res.getString(res.getColumnIndex("id"));
return s;
}
There are two factors that need to be considered when retrieving data from a cursor; which row (position within the cursor) and then column and it's type.
To position the Cursor you use one of the Cursor's move methods. These are :-
move, which moves the cursor relevant to it's current position.
moveToFirst
moveToLast
moveToNext
moveToPosition and
moveToPrevious
All return true if the move could be performed otherwise false.
A cursor can be empty and have no rows, so movetoFirst would return false.
One of the more common usages is :-
while (cursor.moveToNext) {
.... cursor action(s) here.
}
A Cursor will initially be positioned to before the first row i.e. it's position is -1 and can be set using moveToPosition(-1).
The Cursor's getPosition method will retrieve the current position, noting that the first row is position 0.
Column's in rows are accessed according to the offset (which column) and the Cursor's get???? (where ???? represents type) methods are used to retrieve the data from the row/column (there are other get.... methods e.g. getPosition as above). The methods for retrieving the data are:-
getBlob (retrieves a byte array).
getDouble
getFloat
getInt
getLong
getShort
getString
All take a single parameter, an integer that is the column's offset within the cursor (which is not necessarily the offset in the table).
You can use any on any type of data, with the exception of blobs. An attempt to use any, except getBlob, on a BLOB will result in an exception.
However, the results, can vary e.g. if you use get, for example assuming column with offset 0 contains *rumplestilskin then :-
cursor.getString(0) will return rumplestiltskin
cursor.getDouble(0) will return 0 as a double.
cursor.getInt(0) will return 0 as a float.
.....
In short, you should use the appropriate get method, otherwise the results could be confusing.
Hard coding column column offsets, often results in issues therefore it is generally better to use column names in conjunction with the Cursor's getColumnIndex method. This takes a String, the column name, as a parameter and returns the offset of the column.
Applying the above to the question:-
Code that could be useful (i.e. it retrieves data) could be :-
Cursor csr = db.getData(myid);
while (csr.moveToNext) {
long id_of_the_row = csr.getLong(csr.getColumnIndex(yourDatabaseHelperClass.id));
}
Noting:-
if (moveToFirst(myid)) { .... } could be considered more correct but assuming that just a single row exists bot moveToFirst and moveToNext, as coded, produce the same result.
Assuming that the id column is an alias of the rowid, then rowid is stored as a long and should properly be extracted as a long.
yourDatabaseHelperClass.id will not be correct, rather you would replace it with the class that extends SQLiteOpenHelper (the class that contains your getData method), it also assumes that variable id has public access.
Links that may be useful or of interest:-
Cursor
How flexible/restricive are SQLite column types?
(includes examples of how some of the Cursor get???? methods retrieve data)
Are there any methods that assist with resolving common SQLite issues?

Android cursor no step more than 1 record

My query return 198 registers but cursor only read one register.
Why?
The mCount property of Cursor show 198.
This code:
public ArrayList<EnderecoOficina> retornarListaEnderecoOficina(String sCampo,
String sWhere) {
ArrayList<EnderecoOficina> lista = new ArrayList<EnderecoOficina>();
String query = String.format("SELECT %s FROM %s WHERE %s",
sCampo,
MyDBConstants.TABLE_OFICINAS,
sWhere);
SQLiteDatabase db = dbHandler.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
int i = 0;
if (cursor != null && cursor.moveToFirst()){
EnderecoOficina item = new EnderecoOficina(i++,
cursor.getString(0),
cursor.getString(1),
cursor.getString(2));
lista.add(item);
} while (cursor.moveToNext());
cursor.close();
db.close();
return lista;
}
image link (my points not allow attach image here).
I think you're confusing while syntax.
while (cursor.moveToNext());
Will loop without doing anything until the Cursor is empty. I think you wanted a do/while as explained by CommonsWare answer.
In my opinion, this is an unnecessary complicated way to do it. A lot of people don't know how to use Android Cursor. I've seen all kinds of complicated ways to do it (checking for null, moving to first, moving to index...), but this is the simplest way:
try {
// Loop while there are records on the Cursor
while (cursor.moveToNext()) {
EnderecoOficina item = new EnderecoOficina(i++,
cursor.getString(0),
cursor.getString(1),
cursor.getString(2));
lista.add(item);
}
} finally {
// Make sure the cursor is closed no matter what
cursor.close();
}
There's no need to check for null, Android Cursor API never returns a null cursor. You also need to close the Cursor once you've finished with it.

sqlite cursorindexoutofbound exception

in my android application i used the following function to retrieve the column from the table..the table contains value but it has an exception.
public String[] getactivelist(){
Log.v("ppp","getactivelist");
String[] actname=new String[50];
SQLiteDatabase db = this.getWritableDatabase();
Log.v("ppp","dbcrtd");
Cursor cursor = db.rawQuery("SELECT name FROM activelist ORDER BY time ASC", null);
Log.v("ppp","aftrcurser");
int i=0;
Log.v("ppp crsr",cursor.getString(0));
if (cursor.moveToFirst()) {
do {
actname[i]=cursor.getString(0);
Log.v("ppp crsr",cursor.getString(0));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return actname;
}
the log cat shows the following error
04-04 01:19:41.170 2581-2601/com.example.pranavtv.loudspeaker V/pppīš• tryandroid.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
In your line
Log.v("ppp crsr", cursor.getString(0));
You try to get a string from the cursor. If there aren't any lines, it should throw the error observed.
You are calling the following line...
Log.v("ppp crsr",cursor.getString(0));
...before you have moved the position of your Cursor using...
if (cursor.moveToFirst()) {
By default, the position of a Cursor is initially set to be -1 which is before the first valid position as the first position which contains data is position 0.
Simply remove that line (the one before the if(...)) and you should be good to go.
On another note, in your do...while loop you are using...
actname[i]=cursor.getString(0);
...but you never increment i. Consequentially you will only ever modify actname[0] regardless of how many results are returned to the Cursor.

What is The use of moveToFirst () in SQLite Cursors

I am a programming newbie
and I found this piece of code in the internet and it works fine
Cursor c=db.query(DataBase.TB_NAME, new String[] {DataBase.KEY_ROWID,DataBase.KEY_RATE}, DataBase.KEY_ROWID+"= 1", null, null, null, null);
if(c!=null)
{
c.moveToFirst();
}
but I am not able to understand the use of the
if(c!=null)
{
c.moveToFirst();
}
part. What does it do exactly , and if I remove the
if(c!=null) { c.moveToFirst(); }
part, the code doesn't work.
The docs for SQLiteDatabase.query() say that the query methods return:
"A Cursor object, which is positioned before the first entry."
Calling moveToFirst() does two things: it allows you to test whether the query returned an empty set (by testing the return value) and it moves the cursor to the first result (when the set is not empty). Note that to guard against an empty return set, the code you posted should be testing the return value (which it is not doing).
Unlike the call to moveToFirst(), the test for if(c!=null) is useless; query() will either return a Cursor object or it will throw an exception. It will never return null.
if (c.moveToFirst()) {
while(!c.isAfterLast()) { // If you use c.moveToNext() here, you will bypass the first row, which is WRONG
...
c.moveToNext();
}
}
Cursor is not a Row of the result of query. Cursor is an object that can iterate on the result rows of your query. Cursor can moves to each row. .moveToFirst() method move it to the first row of result table.
moveToFirst() method moves the cursor to the first row. It allows to perform a test whether the query returned an empty set or not. Here is a sample of its implementation,
if (cursor.getCount() == 0 || !cursor.moveToFirst()) {
return cursor.getLong(cursor.getColumnIndexOrThrow(ID_COLUMN));
cursor.close();
what macio.Jun says is right!
we have code like below:
String sql = "select id,title,url,singer,view,info from cache where id=" + id;
SQLiteDatabase db = getMaintainer().getReadableDatabase();
Cursor query = db.rawQuery(sql, null);
query.moveToFirst();
while(query.moveToNext()){
DBMusicData entity = new DBMusicData();
entity.setId(query.getString(query.getColumnIndex(FIELD_ID)));
entity.setTitle(query.getString(query.getColumnIndex(FIELD_TITLE)));
entity.setSinger(query.getString(query.getColumnIndex(FIELD_SINGER)));
entity.setTitlepic(query.getString(query.getColumnIndex(FIELD_PICURL)));
entity.setInfoUrl(query.getString(query.getColumnIndex(FIELD_INFO)));
entity.setViews(query.getString(query.getColumnIndex(FIELD_VIEW)));
Log.w(tag, "cache:"+ entity.toString());
}
query.close();
query=null;
db.close();
db=null;
If we have only one record in the cache table, query.moveToFirst(); will cause that no record returns.

Retrieve the contents of first three rows of a android database using a cursor

Currently in my code i'm using a cursor to retrieve the entire database
My code is
public Cursor getAll() {
return (getReadableDatabase().rawQuery(
"SELECT _id, note, amt, dueDate FROM New", null));
}
The function of retrieving the contents is to populate the same in a listview.
Now I want to retrieve the contents of the first three rows of the same database using cursor to display in another listview.
Need Help, Thanks in Advance.
The correct way to do it is to limit the result number to three:
"SELECT _id, note, amt, dueDate FROM New ORDER BY _id LIMIT 3"
Then you just iterate over the cursor (as usual)
Since you've already obtained a Cursor, in order to get the first three rows of the result, you do this:
Cursor cursor = getAll();
cursor.moveToFirst();
int count = 0;
while(!cursor.isAfterLast() && count < 3)
{
// Grab your data here using cursor.getLong(0), cursor.getString(1) etc.
// and store it an array.
count++;
cursor.moveToNext();
}
cursor.close();
You may want to limit the query results to at most three by adding a LIMIT 0,3 statement to your SQL. Having obtained an array of at most three elements containing your records, you can then proceed to place them in the other ListView you are referring to. You do this by adding them to this ListView's source array. Then call the ListView adapter's notifyDataSetChanged method to have it update itself.
So you can do this in two ways:
Create a separate select:
SELECT * FROM Table_Name LIMIT 3;
Select three rows from cursor:
int n = 0;
cursor.moveToFirst();
while (!cur.isAfterLast() && n < 3) {
// Use the data
n++;
cur.moveToNext();
}
cur.close();

Categories

Resources