Query only the first data from a table

2019-04-02 11:38发布

问题:

I'm trying to query only the first data from the table.

Cursor img_cursor = db.query(
                    IMAGE_URL_TABLE_NAME,
                    new String[]{"small_img_url" , "large_img_url"},
                    null",
                    null, null, null, null);

Could somebody tell me how to implement a query where only the first data is retrieved from a table?

Solution

I think I solved the answer:

Cursor img_cursor = db.query(
                    IMAGE_URL_TABLE_NAME,
                    new String[]{"small_img_url" , "large_img_url"},
                    null",
                    null, null, null, null , "1");

I used limit 1 first, but the application crashed. Only if I pass the number as a String will it work. The query has 8 parameters while we're using the limit parameter.

回答1:

limit 1

From Documentation:

public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

so, put your last argument to be "limit 1".

Cursor img_cursor = db.query(
                IMAGE_URL_TABLE_NAME,
                new String[]{"small_img_url" , "large_img_url"},
                null,
                null, null, null, "limit 1");