views:

36

answers:

1

In SQLite, with the standard C interface -- is there an easy way (even if a bit hack-ish) to iterate backwards in the result set? I'm trying to avoid caching the results myself.

A solution I thought about was executing an extra query that would return the reverse results, but I'm not sure how efficient it would be.

+2  A: 

No, the sqlite iteration code is specifically one-way. I'm not aware of even a hack method of achieving what you want, other than caching it all yourself of course :-).

The method of a reverse direction query may work a little bit but you can no more make the reverse query go "forward" (its backwards) than you can make the normal query go backwards.

Once your forward query had gotten to row 50 and your reverse query had gotten to row 20, rows 21 through 49 would be inaccessible to both of them. So you can't really use it as a general purpose browsing method.

Assuming you're trying to achieve a paging mechanism such as displaying records fifty at a time and allowing the user to scroll forwards and backwards through the rows, you may just have to resort to the time-honored approach of running many queries, one at a time, retrieving only fifty rows in each. Something like:

select f1, f2, f3 from tbl limit 50 offset 0
select f1, f2, f3 from tbl limit 50 offset 50
select f1, f2, f3 from tbl limit 50 offset 100

and so forth. You can use the offset and limit to restrict your window onto the table.

paxdiablo