logo       

RE: Template::Plugin::DBI - Using an existing db hand le: msg#00079

Subject: RE: Template::Plugin::DBI - Using an existing db hand le

-----Evan Kaufman wrote-----
From: Evan Kaufman [mailto:evan.kaufman-Re5JQEeQqe8AvxtiuMwx3w@xxxxxxxxxxxxxxxx]
Sent: 27 June 2006 23:15
To: templates-MnLy+PD7ppGDO3lXZvfOKmD2FQJk+8+b@xxxxxxxxxxxxxxxx
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
templates-MnLy+PD7ppGDO3lXZvfOKmD2FQJk+8+b@xxxxxxxxxxxxxxxx http://lists.template-toolkit.org/mailman/listinfo/templates

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
boot-loaders.gr...    php.pear.genera...    debugging.valgr...    kde.redhat.user...    text.xml.xsl.ge...    culture.languag...    hardware.microc...    java.servicemix...    redhat.release....    web.zope.plone....    user-groups.lin...    opendarwin.webk...    video.mjpeg.use...    sysutils.bcfg2....    encryption.gpg....    lx-office.devel...    xfree86.forum/2...    mail.mutt.devel...    acpi.devel/2003...    qnx.openqnx.dev...    network.irc.irs...    freebsd.devel.m...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe