如何在sqlite中使用limit

时间:2013-09-25 11:36:35

标签: perl sqlite dancer

get '/' => sub {
       my $db = connect_db();
       my $sql = 'select id, title, text from entries order by id desc limit 3 offset 4';
       my $sth = $db->prepare($sql) or die $db->errstr;
       $sth->execute or die $sth->errstr;
       template 'show_entries.tt', {
               'msg' => get_flash(),
               'add_entry_url' => uri_for('/add'),
               'entries' => $sth->fetchall_hashref('id'),
       };
};

我想在主页上显示3个主题,但出于某种原因显示全部。我做错了什么?

1 个答案:

答案 0 :(得分:2)

为我工作

$ perl -E'
   use DBI;
   my $dbh = DBI->connect("dbi:SQLite:", undef, undef,
      { PrintError => 0, RaiseError => 1, AutoCommit => 1 });

   $dbh->do("CREATE TEMP TABLE MyTable ( id INT )");
   $dbh->do("INSERT INTO MyTable VALUES (?)", undef, $_) for 1..9;

   my $sql = "SELECT * FROM MyTable LIMIT 3 OFFSET 4";
   my $sth = $dbh->prepare($sql);
   $sth->execute();
   say for keys %{ $sth->fetchall_hashref("id") };
'
5
6
7

或者从命令行客户端

$ sqlite3
SQLite version 3.3.6
Enter ".help" for instructions
sqlite> CREATE TEMP TABLE MyTable ( id INT );
sqlite> INSERT INTO MyTable VALUES (1);
sqlite> INSERT INTO MyTable VALUES (2);
sqlite> INSERT INTO MyTable VALUES (3);
sqlite> INSERT INTO MyTable VALUES (4);
sqlite> INSERT INTO MyTable VALUES (5);
sqlite> INSERT INTO MyTable VALUES (6);
sqlite> INSERT INTO MyTable VALUES (7);
sqlite> INSERT INTO MyTable VALUES (8);
sqlite> INSERT INTO MyTable VALUES (9);
sqlite> SELECT * FROM MyTable LIMIT 3 OFFSET 4;
5
6
7
sqlite> .q

版本:

$ perl -MDBI -E'
   my $dbh = DBI->connect("dbi:SQLite:", undef, undef);
   say for
      $DBD::SQLite::VERSION,
      $dbh->{sqlite_version}
'
1.40
3.7.17