Perl dancer:将数据库信息行传递给模板

时间:2014-12-02 12:42:06

标签: perl dbi dancer

使用教程中的信息我创建了一个方法,该方法从表单中获取信息并检查数据库中输入数据的可用内容:

post '/identry' => sub {
#to cut short the long code the following are the steps
#getting the last time the data has been entered into the database
# to match with the ids entered through the form , I have to display what is available
   in the database for the same position
   my $sql2 = "SELECT a.ID FROM db_test a, db_check b  WHERE 
 a.POSITION=b.RACK||'/'||b.POS AND DOE =TO_TIMESTAMP('$last_entry','dd-mm-yyyy hh24:mi:ss:ff') ORDER BY SUBSTR(a.POSITION,9,3)";

    my $sth2= $dbh->prepare($sql2) or die $dbh->errstr;
    $sth2->execute() or die "$dbh->errstr \n";
    template 'checkid.tt', {
        'checkid_url' => uri_for('/checkid'),
         'entries' => $sth2->fetchall_hashref('ID'),
         };
};

我收到错误

 Warning caught during route execution: DBD::Oracle::st fetchall_hashref failed: ERROR no statement executing (perhaps you need to call execute first) [for Statement "SELECT a.ID FROM db_test a,db_check b  WHERE a.POSITION=b.RACK||'/'||b.POS AND DOE =TO_TIMESTAMP('02-12-2014 12:00:33:000000','dd-mm-yyyy hh24:mi:ss:ff') ORDER BY SUBSTR(a.POSITION,9,3)"] 

模板如下

<% IF entries.size %>
   <% FOREACH ID IN entries %>
       <p><input type="text" name=id1 id="id1" value="<% entries.$ID.0 %>" size =8>
          <input type="text" name=id2 id="id2" value="<% entries.$ID.1 %>" size =8>
       </p>
   <% END %>
<% ELSE %>
 <em> No data available in Database </em>
<% END %>

1 个答案:

答案 0 :(得分:0)

您没有从表中选择其他列。您想只选择ID吗? 如果没有,请在选择查询中包含其他列。

在模板中,获取foreach循环内的值,如下所示:

<input type="text" name=id1 id="id1" value="<% entries.$ID.ID %>" size =8>
<input type="text" name=id1 id="id1" value="<% entries.$ID.OTHERCOLUMN %>" size =8>

当您使用列名时请记住它区分大小写。(仅供参考)

还练习在查询中的每一列使用别名: