1

I am new to SQLite and I don't know how to use limit and offset to select a limit number of data from the database, I mean I know the query phrase, but how to use it in a cursor so I can get those data into a listview?

Currently I am using the code below to query data from the database and show them in a listview but it seems I query too much data for one query and the SQLite fail to grow, so I want to split the query into some smaller ones and do it in one time,someone suggested me to try limit and offset,but I googled it there are really not much about it on the Internet.

Would somebody kindly provide me the guide to this? an example or a tutoral, anything will do,thx

channellist = (ListView) findViewById(R.id.Channel);

        mDB = new ChannelDB(this);

        String[] columns = {mDB.KEY_ID, mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_PATH, mDB.KEY_DBLINK};
        String   table   = mDB.channelS_TABLE;

        c = mDB.getHandle().query(table, columns, null, null, null, null, null);

        startManagingCursor(c);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.channelview,
                c,
                new String[] {mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_DBLINK},
                new int[] {R.id.poster, R.id.channel, R.id.douban});

        adapter.setViewBinder(new ChannelViewBinder(this));

        channellist.setAdapter(adapter);
oratis
  • 818
  • 1
  • 9
  • 29

1 Answers1

3

pass the last argument with number as string like you need fetch 10 then you can do like this way

c = mDB.getHandle().query(table, columns, null, null, null, null, null,"10");

for more reference see How to use the LIMIT argument in an SQLite Query with Android

Community
  • 1
  • 1
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • Hi,@Pratik, I tried this, but I got a SQLite exception **1st ORDER BY term out of range - should be between 1 and 5: , while compiling: SELECT _id, poster, channel, path, dblink FROM channels ORDER BY 10** any idea? – oratis Dec 06 '11 at 11:30
  • yes,thx,but I still got a single page of list but not all items loaded, so shall I creat multiple cursors and run them together? – oratis Dec 06 '11 at 11:41
  • You see my problem is like in this http://stackoverflow.com/questions/8395789/how-to-split-query-up-into-smaller-queries-and-run-them-one-at-a-time-in-android – oratis Dec 06 '11 at 11:50
  • Thx, I got the idea and no longer have this issue – oratis Dec 06 '11 at 12:01