Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have inserted row into the table 'balance_table'.

Java
//insert:
 public long insertinBalance(String new_bal1) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues contentvalues = new ContentValues();
        contentvalues.put(VivzHelper.AVL_BALANCE, new_bal1);
        long id = db.insert(VivzHelper.BALANCE_TABLE, null, contentvalues);
        helper.close();
        return id;
    }

// retrieve column value

 public Cursor balance() {
        SQLiteDatabase db = helper.getReadableDatabase();
        String[] columns = {VivzHelper.BAL_UID, VivzHelper.AVL_BALANCE}; 
        Cursor c1 = db.query(VivzHelper.BALANCE_TABLE, columns,null , null, 
        null,null,null);
        existing_bal = "0";
        if(c1.getCount() > 0){
            existing_bal = (c1.getString(1));
        }
        else
        {
        }
        helper.close();
        return c1;


I wanted to retrieve the value from column 'avl_balance' and pass into a variable 'existing_bal' using the above code. I get error in logcat stating like:Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
How to rectify to get the value passed into variable 'existing_bal'.
Posted
Updated 31-Aug-15 19:33pm
v2

1 solution

Java
if(c1.getCount() > 0){
    existing_bal = (c1.getString(1));
}

should be
Java
if(c1.getCount() > 0){
    existing_bal = (c1.getString(0)); // first element is index zero
}


See http://developer.android.com/reference/android/database/Cursor.html#getString(int)[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900