Adding anything to Section String breaks the android SQLite DB query?

Go To StackoverFlow.com

0

I'm new to Android and SQLite, too. I'm trying to write a method where the DB query gets a specific row from the DB. This version of the method works fine where Selection is set to null:

    public String anotherTry(){
        String[] columns = new String[]{ KEY_SDATE, KEY_SC, KEY_VE };
        Cursor cursor = db.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";
        if (cursor != null){
            cursor.moveToFirst();
            result = result
        + cursor.getString(0) + "\n"
        + cursor.getString(1) + "\n"
            + cursor.getString(2) + "\n";
            return result;
        }
        return null;
    }

but if I put anything in the Selection argument, it crashes. For example:

    public String anotherTry(){
        String[] columns = new String[]{ KEY_SDATE, KEY_SC, KEY_VE };
        Cursor cursor = db.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + "8", null, null, null, null);
        String result = "";


        if (cursor != null){
            cursor.moveToFirst();
            result = result
        + cursor.getString(0) + "\n"
        + cursor.getString(1) + "\n"
            + cursor.getString(2) + "\n";
            return result;
        }
        return null;
    }

What's going wrong here? I'm trying to get the 8th row to be displayed. I've tried all the formatting combos that I can think of: KEY_ROWID + "=" + "8", KEY_ROWID = "8", and so on, but no luck. Thanks for any help!

2012-04-04 16:43
by kirktoon1882
what is the error in logcat - Sunny 2012-04-04 17:27
I think the revelant LogCat line is: android database CursorIndexOutOfBoundsException: Index 0 requested, with a size of - kirktoon1882 2012-04-04 18:45


0

try followin code, i think it wil work for u

Cursor cursor = db.query(DATABASE_TABLE, columns, KEY_ROWID+"=?", new String[] {"8"}, null, null, null);
2012-04-04 17:22
by Chintan Raghwani
no, that didn't work. why can't I put the desired ID number in Selection? Why would you need to put "?" in selection and the new string in Selection Arg - kirktoon1882 2012-04-04 18:52
ok, that's strange. I changed the Selection argument to: KEYROWID="8" and null for SelectionArg. Then Eclipse told me to remove the Final modifier from KEYROWID. I did that and the method at least executes, but only gives me the first row. Which makes me think that with Final removed, it's ignoring the Selection argument and going right to cursor.moveToFirst();. Is that what's happening? How do I fix it to work correctly - kirktoon1882 2012-04-04 19:08


0

try the following code:

Cursor cursor = db.query(DATABASE_TABLE, columns, KEY_ROWID + "=8", null, null, null, null);

or i think that nothing exist in your database at KEY_ID=8 bcoz the CursorIndexOutOfBounds exception comes when the query returns no data.Check if database exists at KEY_ROWID=8

2012-04-04 18:52
by user182022
Thanks to Sunny, user182022 and Chintan Raghwani. The cursor was out of bounds because I had a delete DB method in each launch of the app, but the autoincrement ID kept going. By the time I figured that out I was in the 600's in the ID's with only 10 rows in my DB! And Chintan Raghwani's Selection solution worked the best. Thanks - kirktoon1882 2012-04-05 14:38
Ads