The thing that is out of range is not the column index, but the row
within the Cursor.
When you receive a Cursor from query(), it is positioned *before* the
first row, at position -1 so to speak.
You need to call Cursor.moveToFirst or Cursor.moveToNext to position
it on the first valid item.
Conveniently, those methods return true if successfull, so you could
just replace this:
int numGames = gamesCursor.getCount();
if (numGames > 0){
with this:
if (gamesCursor.moveToNext()){
On Tue, Jun 30, 2009 at 8:10 AM, Rick<[email protected]> wrote:
>
> Hi.
>
> I am having some trouble working with the SQLite database. I am able
> to create the database, add to it, and delete from it with no problem
> by following the Notepad examples, but I am trying to avoid duplicates
> and therefore want to check to see if an entry exists already.
>
> Below is the code I have for testing to see if a game exists. I get
> the correct Log message of "No Games" when there are no games, as well
> as the correct value for column from getColumnIndex which is proven to
> me by the log printout telling me that column = 1.
>
> The error message that I receive if I don't catch the exception says
> "Caused by android.database.CursorIndexOutOfBoundsException: Index -1
> requested, with a size of 1
>
> Where is Index -1 being requested if my call to getColumnIndex is
> returning 1? Thanks for any help.
>
> public boolean haveGame(){
> gamesCursor = dbHelper.fetchAllAssets();
> startManagingCursor(gamesCursor);
> int numGames = gamesCursor.getCount();
> if (numGames > 0){
> int column = gamesCursor.getColumnIndex("hunt_name");
> try {
> String game = gamesCursor.getString(column);
> Log.i("GAMES","Game: " + game);
> } catch (CursorIndexOutOfBoundsException cioob){
> Log.i("GAMES","Col: "+Integer.toString(column)+" | OOB
> Exception");
> }
> return false;
> } else {
> Log.i("GAMES","No Games");
> return false;
> }
> }
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---