sqlite cursorindexoutofbound exception - android

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.

Related

CursorIndexOutOfBoundsException on SQLite get

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")

Getting all database entries from android database

I have an android project, with a database where entries are logged as pairs with the same ID.
I have two for loops, one to get all entries of a certain ID, and one to get all entries in the table. These are then automatically populated into a listview.
The problem I have is that the program crashes when I attempt to use the forloop that gets all entries from the database, but it works perfectly fine when I use the for loop that wants all entries with the same ID. I have been commenting out the for loop im not using.
The for loops:
List<Assignment> list = db.getPickupDelivery(1);
for (int i = 0; i < list.size();i++)
{
allDeliveries.add(list.get(i));
}
List<Assignment> list = db.getAllAssignments();
for (int i = 0; i < list.size();i++)
{
allDeliveries.add(list.get(i));
}
The methods they call:
public List<Assignment> getPickupDelivery(int i) {
List<Assignment> assignments = new LinkedList<Assignment>();
//bygg query
String query = "SELECT * FROM " + TABLE_ASSIGNMENTS + " WHERE id = " + i;
//fa referens
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
//iterera och bygg och lagg till
Assignment assignment = null;
if (cursor.moveToFirst()) {
do {
assignment = new Assignment();
assignment.setID(Integer.parseInt(cursor.getString(0)));
assignment.setType(cursor.getString(1));
assignment.setSenderreceiver(cursor.getString(2));
assignment.setAdress(cursor.getString(3));
assignment.setTime(cursor.getString(4));
assignment.setZipcode(cursor.getString(5));
assignment.setContact(cursor.getString(6));
assignment.setPhone(cursor.getString(7));
assignment.setInstructions(cursor.getString(8));
//lagg till
assignments.add(assignment);
} while (cursor.moveToNext());
}
cursor.close();
Log.d("getPickupDelivery()", assignments.toString());
return assignments;
}
public List<Assignment> getAllAssignments() {
List<Assignment> assignments = new LinkedList<Assignment>();
//bygg query
String query = "SELECT * FROM " + TABLE_ASSIGNMENTS;
//fa referens
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
//iterera och bygg och lagg till
Assignment assignment = null;
if (cursor.moveToFirst()) {
do {
assignment = new Assignment();
assignment.setID(Integer.parseInt(cursor.getString(0)));
assignment.setType(cursor.getString(1));
assignment.setSenderreceiver(cursor.getString(2));
assignment.setAdress(cursor.getString(3));
assignment.setTime(cursor.getString(4));
assignment.setZipcode(cursor.getString(5));
assignment.setContact(cursor.getString(6));
assignment.setPhone(cursor.getString(7));
assignment.setInstructions(cursor.getString(8));
//lagg till
assignments.add(assignment);
} while (cursor.moveToNext());
}
cursor.close();
Log.d("getAllAssignments()", assignments.toString());
return assignments;
}
The error I get at the time of the crash is a an error that says "...MainActivity}: java.lang.NumberFormatException: Invalid int: "null""
My guess is that in your database definition, you didn't specify the ID column as INTEGER NOT NULL. So when you select every row, one or more of those rows has a null ID, which you're trying to parse as an int here:
assignment.setID(Integer.parseInt(cursor.getString(0)));
Change your database definition to require that the ID column is not null. You'll need to uninstall or clear data on your app so the database is recreated.
Also another tip - Android's SQLITE implementation will enforce that you have a column called "_id", so I'd suggest using that, not "id". Otherwise you'll end up with an extra column you don't need.
In your code:
assignment.setID(Integer.parseInt(cursor.getString(0)));
if cursor.getString(0) returns null, then Integer.parseInt(String str) will throw NumberFormatException.
Check this line or you can use try-catch and print the appropriate message in catch if you'll get NumberFormatException.
The exception is the hint. It says that a string null is being parsed as a number and this fails.
In your code there the place where it happens is:
assignment.setID(Integer.parseInt(cursor.getString(0)));
First you get a string from the cursor and then you pass it to Integer.parseInt for parsing.
So the cause of the problem is that you expect there to be no null values in the first column but there is at least one.
Also, your code is unnecessarily complex and inefficient. Instead of Integer.parseInt(cursor.getString(0)) you could write cursor.getInt(0) to directly get an int. This is also likely to throw an error on the null value though (but the error message might be more informative).

getting table info(exist/not) from cursor

in my activity ineed to check whether the table exist or not for that im using
Cursor cursor = marksdb.rawQuery("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='"+classt+"'", null);
im getting 1 if table exist 0 otherwise
for that i need check value in cursor for that im using
if(cursor.getCount()==1){
// get values from cursor here
callclasstb();
}
else{
tv.setVisibility(View.VISIBLE);
subjectet.setEnabled(false);
markset.setEnabled(false);
markssp.setEnabled(false);
}
but in all cases im getting value 1 because getcout() returns a value 1 r 0 and
callclasstb();
is not executing what condition i need to write in if{....} to make it execute
cursor.getCount() in your case will always return 1 (the number of "rows" in your resultset). you need to figure out what was returned via the cursor.
Cursor cursor = marksdb.rawQuery(...);
cursor.moveToFirst(); // first "row"
int nTableExists = cursor.getInt(0);
cursor.close();
if (nTableExists) { // != 0
...do something...
}

Error when accessing cursor elements

I'm trying to get all the IDs in the table by cursor. The table has 4 rows, and when I try to access the cursor by any index aside from 0 , the App crashes.
Guys, the problem still exists and even c.getInt(0) doesn't work...I really dont know where my mistake is???
the logcat also suggests that the error might be comes from
Toast.makeText(getApplicationContext(), "id="+dbd.getIDs()[0], Toast.LENGTH_SHORT).show();
I mean c.getint(0) returns the id, c.getint(2) returns error. Here is the code:
public int []getIDs() {
SQLiteDatabase db = this.getReadableDatabase();
SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT " + BaseColumns._ID + " FROM Demo ", null);
int []r = new int[c.getCount()];
c.moveToFirst();
do{
r[c.getPosition()] = c.getInt(0);
}while(c.moveToNext());
c.close();
db.close();
return r;
}
Your select is a projection onto the ID column (select columns from ..., columns are the column ids you are interested in, and you specified just one). Thus the answer just has one column, namely the ID. Any access to columns with index > 0 will not work.
To access the other columns name them in the projection in your query.
c.getInt(0) return only value of first colunn from current row.
try this code:
do{
int r = c.getInt(0);
Log.d("Your class name","id value = "+r);
}while(c.moveToNext());
You can imagine Cursor as a table. There are rows and columns. And a cursor is pointing on a particular row in this table. Thus, to get all id's you should move across all rows.
c.getInt(ind) In this statement index is pointing on the column with index ind. Thus, in your code you try to get the second and third column and according to your code there is no these column.
To get a proper code you should create a loop and traverse all rows of your cursor. Also you should use c.getInt(0) to get the columns values.
Assuming you are selecting the appropriate Data, your problem is that you're not preparing the Cursor to be iterated.
Before iterating, call:
c.moveToFirst();
Like this:
int []r = new int [c.getCount()];
c.moveToFirst();
do{
r[c.getPosition()] = c.getInt(0);
}while(c.moveToNext());
c.close();
db.close();
return r;
This is well indicated in LogCat. I can't remember how it's put, but the message is very suggestive. Please post as much of the Log as possible, especially the good bits.
Also, I modified 'r' to be an array. As it was you were only returning the value of the last row.

Cursor out of bounds exception when inserting values to Database - Android

I'm trying to insert values from a cursor to my database, but I get an error.
Here's what I do -
Cursor getUserDrinks = myDbHelper.getUserDrinks();
if(getUserDrinks != null)
{
if (getUserDrinks.moveToFirst()){
do{
ContentValues initialValues = new ContentValues();
initialValues.put("code", getUserDrinks.getInt(getUserDrinks.getColumnIndex("code")));
initialValues.put("imgstr", getUserDrinks.getString(getUserDrinks.getColumnIndex("imgstr")));
initialValues.put("name", getUserDrinks.getString(getUserDrinks.getColumnIndex("name")));
initialValues.put("alc", getUserDrinks.getDouble(getUserDrinks.getColumnIndex("alc")));
initialValues.put("user", 1);
try {
myDataBase.beginTransaction();
myDataBase.insert("drinks", null, initialValues);
myDataBase.setTransactionSuccessful();
}
finally {
myDataBase.endTransaction();
}
}while(getUserDrinks.moveToNext());
}
}
But it gives me a CursorIndexOutOfBoundsException. Here's the Log -
04-30 13:22:35.363: ERROR/AndroidRuntime(19161): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.alco/android.alco.main}: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
I know that the Cursor is not empty.
What am I doing wrong?
The problem is that moveToNext() returns false when you are already positioned after the last element and want go further. Range of valid positions is from -1 to elementCount inclusive, so the last position is legitimate, but there's no element. This is what really happens in your code if the cursor has two positions:
You call moveToFirst(), cursor is positioned at the first element with index 0.
You read the values and call moveToNext(). Movement succeeds, you are positioned at index 1.
You read the values and call moveToNext(). Movement succeeds, you are positioned at index 2, which is beyond the last element.
You try to read the values and fail because there is no element with index 2.
What you need to do is the following:
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
//Read the contents
cursor.moveToNext(); //Advance to the next element
}

Categories

Resources