Limiting a SQLite query in android [closed]
Asked Answered
I

4

5

I am using an SQLite database in my android application, and I have a function which selects the rows from a certain table:

public Cursor getAllDiscounts() {
    // return db.query(table, columns, selection, selectionArgs, groupBy,
    // having, orderBy);
    return db.query(DATABASE_TABLE, new String[] { KEY_ROWID,
            KEY_PORTALNAME, KEY_TITLE, KEY_TITLESHORT, KEY_DEALURL,
            KEY_ENDDATE, KEY_COORDS, KEY_CITY, KEY_IMAGEDEAL,
            KEY_CLICKPRICE, KEY_CONVERSIONPERCENTAGE, KEY_FINALPRICE,
            KEY_ORIGINALPRICE, KEY_SALES, KEY_KATEGORIJA, KEY_POPUST },
            null, null, null, null, null, null);
}

What I want to do, is to select rows starting at a certain row and limit the result to another number. So, for instance, I want to start at the tenth row and select the following 20 rows. I tried it like this:

public Cursor getAllDiscounts() {
    return db.query(DATABASE_TABLE, new String[] { KEY_ROWID,
            KEY_PORTALNAME, KEY_TITLE, KEY_TITLESHORT, KEY_DEALURL,
            KEY_ENDDATE, KEY_COORDS, KEY_CITY, KEY_IMAGEDEAL,
            KEY_CLICKPRICE, KEY_CONVERSIONPERCENTAGE, KEY_FINALPRICE,
            KEY_ORIGINALPRICE, KEY_SALES, KEY_KATEGORIJA, KEY_POPUST },
            null, null, null, null, null, "10, 20");
}

but the application crashes. I also tried with "LIMIT 10,20" instead of "10, 20", but that doesn't work either. Anyone?

Irremeable answered 3/12, 2012 at 19:28 Comment(2)
Attach the logcat of the error when it crashes please.Shimkus
Duplicate of #6362168, perhaps?Node
R
9

the limit clause should be "10,20" with no space between the coma and the 20.

public Cursor getAllDiscounts() {
    return db.query(DATABASE_TABLE, new String[] { KEY_ROWID,
            KEY_PORTALNAME, KEY_TITLE, KEY_TITLESHORT, KEY_DEALURL,
            KEY_ENDDATE, KEY_COORDS, KEY_CITY, KEY_IMAGEDEAL,
            KEY_CLICKPRICE, KEY_CONVERSIONPERCENTAGE, KEY_FINALPRICE,
            KEY_ORIGINALPRICE, KEY_SALES, KEY_KATEGORIJA, KEY_POPUST },
            null, null, null, null, null, "10,20");
}
Resurrection answered 27/2, 2014 at 12:2 Comment(1)
NOTE: The limit string is of the form: "<offset>,<limit>". So using the original string " limit 10 offset 20" would result in "20,10"Tricky
D
4

Use "limit 10 offset 20" as limit clause.

    public Cursor getAllDiscounts() {
    return db.query(DATABASE_TABLE, new String[] { KEY_ROWID,
            KEY_PORTALNAME, KEY_TITLE, KEY_TITLESHORT, KEY_DEALURL,
            KEY_ENDDATE, KEY_COORDS, KEY_CITY, KEY_IMAGEDEAL,
            KEY_CLICKPRICE, KEY_CONVERSIONPERCENTAGE, KEY_FINALPRICE,
            KEY_ORIGINALPRICE, KEY_SALES, KEY_KATEGORIJA, KEY_POPUST },
            null, null, null, null, null, " limit 10 offset 20");
}
Destinee answered 3/12, 2012 at 19:46 Comment(0)
T
4

Try this:

db.rawQuery("SELECT * FROM " + TABLE_NAME + " ORDER BY " + ORDER_BY + " LIMIT 0, 20", NULL);
Taite answered 3/12, 2012 at 19:53 Comment(0)
L
0

db.rawQuery("SELECT * FROM com_kkorm_data_Entity WHERE id!=0 ORDER BY id LIMIT 0,2",null);

Maybe my sql can help you understand LIMITsql

Leon answered 23/2, 2014 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.