Brethren,
I want to access a csv datafile from a template for the Template Toolkit. I have managed to read from a "real" database, using
Template::Plugin::DBI as follows:
[% USE DBI('dbi:ADO:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/pr
+ojekte/vdx/daten/users.mdb', '', '') -%]
[% FOREACH user = DBI.query( 'SELECT * FROM users' ) -%]
[% user.id %] [% user.name %]
[% END -%]
That is simple and straightforward. Now I have this script, that uses
DBD::CSV to access a textfile.
use strict;
use warnings;
use DBI;
use DBD::CSV;
my $dbh = DBI->connect(qq{DBI:CSV:csv_sep_char=\\;});
$dbh->{'csv_tables'}->{'users'} =
{
'file' => 'c:/projekte/vdx/daten/users.txt',
'col_names' => ["id", "name"]
};
my $sth = $dbh->prepare("SELECT * FROM users");
$sth->execute;
while( my $row = $sth->fetchrow_hashref )
{
print $row->{id}, ";", $row->{name}, "\n";
}
I want to do the same with a template like the one above. But I cannot figure out how to pass the additional parameters into
Template::Plugin::DBI, namely the filename and the fieldnames. All my searching was fruitless.
Btw, I know there is
Template::Plugin::Datafile, but this plugin won't let me specify the fieldnames.
I'm pretty sure I could write my own plugin to interface with
DBD::CSV, but I don't want to if I don't have to. Any suggestions?
Thanks in advance.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.