Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Global symbol "$prepare" requires explicit package name

by stevieb (Canon)
on Sep 18, 2019 at 20:10 UTC ( #11106353=note: print w/replies, xml ) Need Help??


in reply to Global symbol "$prepare" requires explicit package name

In this line:

$sth2 = $dbh->$prepare("update $schema.$table set $date_col = $date_ne +w where $where_col = 1;") or die;

...which is the third-to-last line of your program, you prepend the prepare() method with a scalar sigal ($). Removing that sigil will at least get you past that error.

Replies are listed 'Best First'.
Re^2: Global symbol "$prepare" requires explicit package name
by tukusejssirs (Beadle) on Sep 19, 2019 at 06:41 UTC

    Youíre right. I missed that.

    But still it does not work; it throws the following error on $sth2->execute(); line (it was output doubled like this with the same line number):

    DBD::Pg::st execute failed: execute called with an unbound placeholder + at ./refresh_dates.pl line nn. DBD::Pg::st execute failed: execute called with an unbound placeholder + at ./refresh_dates.pl line nn.

      Try

      $sth2 = $dbh->$prepare("update $schema.$table set $date_col = ? where $where_col = ?;") or die; $sth2->execute($date_new,1);
      poj

        And could someone tell why the following code does not work? It does not work either with or without placeholders, but with different errors.

        In this case, I want to select all unique dates from timestamps. It works as expected with select distinct cast(date as date) from table; issued directly in psql.

        # With placeholders my $sth_test = $dbh->prepare("select distinct cast($date_col as date) +from ?;") or die; $sth_test->execute($schema.$table); # Errors (on execute()) INFO: The database has been opened successfully. DBD::Pg::st execute failed: ERROR: syntax error at or near "$1" LINE 1: select distinct cast(date as date) from $1; ^ at ./refresh_ +dates.pl line n. DBD::Pg::st execute failed: ERROR: syntax error at or near "$1" LINE 1: select distinct cast(date as date) from $1; ^ at ./refresh_ +dates.pl line n. # Without placeholders my $sth_test = $dbh->prepare("select distinct cast($date_col as date) +from $schema.$table;") or die; $sth_test->execute(); $dbh->disconnect(); # Error (on disconnect) DBI::db=HASH(0x32cec98)->disconnect invalidates 1 active statement han +dle (either destroy statement handles or call finish on them before d +isconnecting) at ./refresh_dates.pl line n.
      In addition to what poj said, instead of
      $sth2 = $dbh->prepare("update $what$ever");
      to see what's going on, write
      my $sqltext = "update $what$ever"; print qq(SQL query is: "$sqltext"); $sth2 = $dbh->prepare($sqltext);
      Perhaps there's a stray question mark in your $date_new

        Indeed poj, it works now.

        Could you tell me please, why does it need to have ? (I believe these question marks are those placeholders from the error message) in the prepare part? And why I could not run $dbh->execute("update $schema.$table set $date_col = $date_new where $where_col = 1;") or die; directly? In this particular code I donít need neither $sth handle for reuse nor the placeholders to re-execute the code (as the variables will be all the same in all the loop cycles).<\p>

        Thank you very much!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2020-10-20 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (210 votes). Check out past polls.

    Notices?