2017-09-17 3 views
8

J'utilise Perl dancer2 pour écrire une application. L'exécution de la requête select dans mysql affiche tous les enregistrements. Mais la même requête exécutée dans dancer2 et template toolkit n'affiche que des enregistrements uniques.template toolkit affiche une ligne unique

Par exemple. 34 enregistrements récupérés lorsque cela est exécuté dans le client mysql.

select timing.time as Time, 
      church.church_name as Church_Name, 
      church.church_address as Address, 
      language.language_name as Language, 
      denomination.denomination_name as Denomination, 
      city.city_name as City, 
      state.state_name as State, 
      country.country_name as Country 
     from church 
     join country on church.church_countryid=country_id 
     join state on church.church_stateid=state.state_id 
     join city on church.church_cityid=city.city_id 
     join church_mass_timing on church.church_id=church_mass_timing.church_id 
     join timing on church_mass_timing.time_id=timing.time_id 
     join language on church_mass_timing.language_id=language.language_id 
     join denomination on church.church_denominationid=denomination.denomination_id 
    order by church.church_name, 
      timing.time; 

La même requête dans Dancer avec Template Toolkit renvoie 11 enregistrements.

get '/church_list' => sub { 
my $db = connect_db(); 
my $sql='select timing.time as Time, 
       church.church_name as Church_Name, 
       church.church_address as Address, 
       language.language_name as Language, 
       denomination.denomination_name as Denomination, 
       city.city_name as City, 
       state.state_name as State, 
       country.country_name as Country 
      from church 
      join country on church.church_countryid=country_id 
      join state on church.church_stateid=state.state_id 
      join city on church.church_cityid=city.city_id 
      join church_mass_timing on church.church_id=church_mass_timing.church_id 
      join timing on church_mass_timing.time_id=timing.time_id 
      join language on church_mass_timing.language_id=language.language_id 
      join denomination on church.church_denominationid=denomination.denomination_id 
     order by church.church_name, 
       timing.time'; 
my $sth = $db->prepare($sql) or die $db->errstr; 
$sth->execute or die $sth->errstr; 
template 'church_list.tt' => { 'title' => 'church list', 
    'site' => 'Church Timings', 
    'entries' => $sth->fetchall_hashref('Time'), 
}; 
};  

c'est le church_list.tt

<% INCLUDE header.tt %> 
<ul id="content"> 
<button id="btnExport">Export to xls</button> 
<br /> 
<br /> 
<div id="table_wrapper"> 

<% IF entries.size %> 
<table border="1" cellspacing="2" widht="100%"> 
<tr> 
<th><center>Time</center></th> 
<th><center>Church name</center></th> 
<th><center>Address</center></th> 
<th><center>Language</center></th> 
<th><center>Denomination</center></th> 
<th><center>City</center></th> 
<th><center>State</center></th> 
<th><center>Country</center></th> 
</tr> 
<% FOREACH id IN entries.keys %> 
<tr> 
<td><% entries.$id.Time %></td> 
<td><% entries.$id.Church_Name %></td> 
<td><% entries.$id.Address %></td> 
<td><% entries.$id.language %></td> 
<td><% entries.$id.denomination %></td> 
<td><% entries.$id.city %></td> 
<td><% entries.$id.state %></td> 
<td><% entries.$id.country %></td> 
</tr> 
<% END %> 
</table> 
</div> 
<% ELSE %> 
<li><em>No entries here so far.</em> 
<% END %> 
</ul> 

Merci.

+0

À quoi sert le «PRI»? Il n'y a pas de colonne avec ce nom, et les docs DBI ne s'y réfèrent pas comme cas particulier. Il ne peut pas non plus être traité comme un nombre, donc il devrait lancer une erreur. – simbabque

+0

Désolé pour l'erreur. Cela aurait dû être Time 'entries' => $ sth-> fetchall_hashref ('Time'), –

Répondre

9

Veuillez prendre note que fetchall_hashref renvoie hashref {FieldValue => FieldsHash}.

Lorsque certaines valeurs de champ ne sont pas uniques, vous avez manqué certains FieldHashes (chaque enregistrement suivant ayant la même valeur Time écrasera le hashref précédent).

Veuillez utiliser des tableaux de lignes (selectall_arrayref) dans ces cas.

P.S .: L'heure dans cette requête n'est pas toujours unique (parfois deux enregistrements peuvent être ajoutés simultanément).

+3

'$ dbh-> selectall_arrayref ($ sql, {Slice => {}})', pour être spécifique. Cela produit un AoH. – ikegami