how to query sqlite for certain rows, i.e. dividing it into pages (perl DBI)

妖精的绣舞 提交于 2019-12-05 10:16:24

Using the LIMIT/OFFSET construction will show pages, but the OFFSET makes the query inefficient, and makes the page contents move off when the data changes.

It is more efficient and consistent if the next page starts the query at the position where the last one ended, like this:

SELECT *
FROM mytable
ORDER BY mycolumn
WHERE mycolumn > :lastvalue
LIMIT 25

This implies that your links are not /webapp?Page=N but /webapp?StartAfter=LastKey.

This is explained in detail on the Scrolling Cursor page.

You should do something like this:

SELECT column FROM table ORDER BY somethingelse LIMIT 0, 25

and when the user clicks on page 2, you should do:

SELECT column FROM table ORDER BY somethingelse LIMIT 25, 50

and so on..

You'd most likely be using the LIMIT and OFFSET keywords, something like this:

$sth->prepare("SELECT foo FROM bar WHERE something LIMIT ? OFFSET ?");
$sth->execute($limit, $offset);
while ( my @row = $sth->fetchrow_array ) { # loop contains 25 items

The $limit and $offset variables would be controlled by the parameters passed to your script by html/cgi/whatever features.

Pagination is one of those problems a lot of CPAN modules have already solved. If you're using straight SQL, you could look at something like DBIx::Pager. You might also want to check out something like Data::Pageset to help you manage creating the links to your various pages. If you're using DBIx::Class (which is an excellent tool) for your SQL queries, then DBIx::Class::ResultSet::Data::Pageset will make this very easy for you.

Essentially handling the SQL is one end of it, but you'll also need to solve various problems in the templating aspect of it. I'd encourage you to have a look at these modules and maybe even poke around CPAN a little bit more to see where somebody else has already done the heavy lifting for you with respect to pagination.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!