Title: RE: [Templates] Template::Plugin::DBI - Using an existing db handle

-----Evan Kaufman wrote-----
From: Evan Kaufman [mailto:[EMAIL PROTECTED]]
Sent: 27 June 2006 23:15
To: [email protected]
Subject: [Templates] Template::Plugin::DBI - Using an existing db handle


> I'm fairly new to TT, but its significantly simplified a large project I'm working on.  I am,
> however, having issues with the DBI plugin, specifically using an already existing connection from
> the calling script for some simple queries.
>
> The documentation for some of these plugins are fairly sparse, they show plenty about the TT
> directives, but not much of the perl code used to implement it, so this may well be something
> simple.  I'm wanting to use an existing DBI handle for a simple predefined query:

Andy and I are working on a new version following the split of the DBI plugin from the core.  I will see if we can add some better examples to the documentation.

> [% USE DBI( dbh = global_dbh ) %]
> <select name="genre">
> [% FOREACH result = DBI.query($genre_query) %]

This should be [% FOREACH result = DBI.query(genre_query) %]

[% foo = "bar" %]
[% bar = "baz" %]
[% $foo %]       

Prints "baz"

So in your case you are looking for the contents of the variable "SELECT ID, Desc FROM Table;" which is empty.

>   <option value="[% result.ID %]">[% result.Desc %]</option>
> [% END %]

> And then, on the perl side, I specify the dbh, and the query:

> my $dbh = DBI->connect("dsn-stuff");
> $tt->process("page.html",
>  {
>    global_dbh => \$dbh,
>    genre_query => "SELECT ID, Desc FROM Table;"
>  }
> );

Dave has already pointed out that you don't need to take a reference to $dbh.

You should always check the return from process and see if anything broke.  You should do this:

$tt->process('page.html', { ........ }) || die $tt->error();

You may want to handle the error rather than just calling die.

> Alas, when I request the page, I get nothing, and the httpd error log has this:

> [Tue Jun 27 17:06:44 2006] [error] [client 71.2.112.154] undef error - Can't call method "execute" 
> on an undefined value at /usr/lib/perl5/vendor_perl/5.8.8/i386-linux/Template/Plugin/DBI.pm
> line 268., referer: http://pe6300.psx.net/catalog/

> This doesn't make sense to me, because the query() method should do a prepare and an execute,
> right?

You found an interesting feature (bug) which I will look at in the next release, which that the error handling seems to be suspect when the query that is passed to the query method is invalid.

Hope this helps

S

_______________________________________________
templates mailing list
[email protected] http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to