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.
# Or:
Can't locate object method "execute" via package "DBI::db" at ./refresh_dates.pl line nn.
####
Global symbol "$prepare" requires explicit package name at ./refresh_dates.pl line nn.
Execution of ./refresh_dates.pl aborted due to compilation errors.
####
my $sth2 = $dbh->$prepare("update $schema.$table set $date_col = $date_new where $where_col = 1;") or die;
$sth2->execute();
####
#!/bin/perl
use strict;
use warnings;
use lib ('./');
use mylib;
use DateTime;
use DateTime::Duration;
use Time::Piece;
use POSIX qw(strftime);
# User-dependent variables
my $db_driver = 'Pg'; # `Pg` for psql, `mysql` for mysql
my $username = 'postgres'; # Database user name
my $password = 'password'; # Database user password
my $database = 'mydb'; # Database name
my $schema_table = 'myschema.mytable'; # Schema.table
# Other variables
my $schema = $schema_table;
my $table = $schema_table;
$schema =~ s/^([^.]*).*$/$1/;
$table =~ s/^[^.]*.([^.]*)$/$1/;
if ($schema_table =~ /\./){
$schema =~ s/^([^.]*).*$/$1/;
$table =~ s/^[^.]*\.([^.]*)$/$1/;
} else {
$schema = 'public';
$table = $schema_table;
$schema_table = '$schema.$table';
}
my $date_col = "date";
my $where_col = "id";
my ($hours, $minutes, $seconds);
my (@date_time, $temp, $date_new);
my ($year, $month, $day);
my ($date_tbl, time_tbl);
my ($dsn, $dbh, $sth, $sth2);
# Open the database
$dsn = "DBI:$db_driver:dbname = $database";
$dbh = DBI->connect($dsn, $user, $password, { RaiseError => 1 }) or die RED, "ERROR: The database could not be opened.\n $DBI::errstr\n Stopped$!";
print "INFO: The database has been opened successfully.\n";
# Get time from DB
$sth = $dbh->prepare("select $date_col from $schema.$table where $where_col = 1;") or die;
$sth->execute();
$sth->bind_col(1, \$temp);
while ($sth->fetch()) {
$date_time[0] = $temp;
}
# Get date
$date_tbl = $temp;
$temp =~ /([^-]+)-([^-]+)-([^-]+) /;
($year, $month, $day) = ($1, $2, $3);
# Get hours, minutes, seconds from time
$time_tbl = $temp;
$time_tbl =~ / ([^:]+):([^:]+):([^:]+)[+]/;
($hours, $minutes, $seconds) = ($1, $2, $3);
# Set new date
$date_new = DateTime->today->set_time_zone('Europe/London')->set_hour($hours)->set_minute($minutes)->set_second($seconds)->strftime("%F %T%z");
# Update the dates
$sth2 = $dbh->$prepare("update $schema.$table set $date_col = $date_new where $where_col = 1;") or die;
$sth2->execute();
# Close the database
$dbh->disconnect() or die RED, "ERROR: The database could not be disconnected.\n $DBI::errstr\n Stopped$!";