Thanks for the suggestion MyLib.pm is
package MyLib; use Mojo::Base 'Mojolicious'; use Mojo::mysql; #my %connections; # This method will run once at server start sub startup { my $self = shift; # Router my $r = $self->routes; # Normal route to controller $r->get('/')->to( template => 'index' ); $r->get('/libmap'); #->to(controller => 'libmap'); $r->get('/invtot')->to( controller => 'invtot', title => 'Paper journals from the library', cb => sub { $self->connect_invtot() } ); if ( exists $ENV{PAR_TEMP} && $^O eq "MSWin32" ) { system qw(start http://localhost:3000/invtot); } $r->post('/invtot')->to( controller => 'Invtot', action => 'post' +); } sub connect_invtot { my $self = shift; #return if $connections{db1}; # Load configuration from hash returned by config file my $file; if ( $ENV{PAR_TEMP} ) { print $ENV{PAR_TEMP}, "\n"; $file = $ENV{PAR_TEMP} . "/inc/script/my_lib.conf"; } else { $file = 'my_lib.conf'; } my $config = $self->plugin( 'Config', { file => $file } ); # Configure the application $self->secrets( $config->{secrets} ); $self->log->debug("connecting..."); $self->helper(db1 => sub {state $db1 = Mojo::mysql->strict_mode('m +ysql://'. $config->{username} . ':'. $config->{password} . '@my.../d +okpe_i01' )}); $self->db1->options({ 'pg_enable_utf8' => 1, AutoCommit => 1, PrintError => 0, RaiseError => 1 }); } 1;
In my Invtot.pm controller the post method is
sub post { my $c = shift; #get data from the form and build sql in $sql{$key} my $stm = $c->app->db1->db->prepare_cached( $sql{$key} ) or die $c->app->db1->db->errstr; # unless(exists $sql{$ +key}); $toFind = format_search_arg( $toFind, $mode ); $c->app->log->debug( $mode . " " . $toFind ); $stm->execute($toFind) or die $stm->errstr; $c->stash( stm => $stm, for_abo => $for_abo ); }
But this failed with Can't locate object method "db1" via package "MyLib" at ...

Thanks for any help


In reply to Re^2: Mojolicious and connection to a database by frazap
in thread Mojolicious and connection to a database by frazap

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.