Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: How can I get a database handler with Mojolicious::Plugin::Database

by frazap (Monk)
on Apr 03, 2019 at 09:11 UTC ( [id://1232069]=note: print w/replies, xml ) Need Help??


in reply to Re: How can I get a database handler with Mojolicious::Plugin::Database
in thread How can I get a database handler with Mojolicious::Plugin::Database

Hi Veltro

Thanks for the suggestion. Eventually, the database handler was found with $c->app->_dbh_db(); Strange that the doc of the module does not describe this...

I'm that code, which works
use Mojolicious::Lite; use Mojolicious::Plugin::Database; use Data::Dumper; plugin 'Database' => { dsn => 'dbi:mysql:host=m...h:dbname=d....', username => 'd....r', password => '.....', options => { 'pg_enable_utf8' => 1, AutoCommit => 1, PrintErr +or =>0, RaiseError => 1 }, helper => 'db', }; get '/invtot' => sub { my $c = shift; $c->render("invtot"); }; post '/invtot' => sub { my $c = shift; my $p = $c->req->body_params->to_hash; print Dumper $p; print Dumper $c->app->_dbh_db(); my $dbh = $c->app->_dbh_db(); my $st = $dbh->prepare("SELECT * FROM jrn WHERE ti like ? ORDER BY + tri ASC") or die $dbh->errstr ; if (length $p->{'ztSearch'} > 0) { $st->execute($p->{'ztSearch'} . '%'); my $ar; while ($ar = $st->fetchrow_arrayref) { my @values = map {defined $_ ? ($_) : ('') } @$ar; print join(" ", @values), "\n"; } $st->finish; } }; app->start; __DATA__ #as above

Now, I would prefer to have my statements prepared as the start of the application, one time after the connection is made, not at each page displayed. What's the best way to do this?

frazap

Replies are listed 'Best First'.
Re^3: How can I get a database handler with Mojolicious::Plugin::Database
by Veltro (Hermit) on Apr 03, 2019 at 10:31 UTC
    the database handler was found with $c->app->_dbh_db();

    The underscore often implies that the developer of the module meant these as 'private' (or internal) functions (and therefor may not be documented). I believe that that is also the case here. I looked at the source and there is definitely a helper created by the name 'db'. Try to use the method from the previous link that I gave you: A Simple Mojolicious/DBI Example

      Thanks for the link

      And yes, the correct way was just $controller->db to have the database handler

Re^3: How can I get a database handler with Mojolicious::Plugin::Database
by Corion (Patriarch) on Apr 03, 2019 at 09:27 UTC

    If you are content with preparing them on the first invocation, you can use either ->prepare_cached or use the state keyword:

    post '/invtot' => sub { state $st = $dbh->prepare(...); $st->execute(...); };

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1232069]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-19 17:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found