1
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 个主题,但由于某种原因显示全部。我做错了什么?

4

1 回答 1

2

Works for me

$ 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

Or from the command line client

$ 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

Versions:

$ perl -MDBI -E'
   my $dbh = DBI->connect("dbi:SQLite:", undef, undef);
   say for
      $DBD::SQLite::VERSION,
      $dbh->{sqlite_version}
'
1.40
3.7.17
于 2013-09-25T16:57:20.680 回答