此脚本适用于所有不需要 % 通配符的命令。当我尝试包装 ? 在 %?% 中产生绑定错误。我还尝试在运行脚本时将 %value% 添加到参数中,它也会产生相同的错误。有没有办法将参数替换为 perl 中的 SQL DBI 命令?
提前致谢。
#!/usr/bin/perl
use DBI;
use Text::Diff;
use File::stat;
use Fcntl ':flock';
open (LOCK_FH, $0) || die "$0 for lock: $!\n";
flock (LOCK_FH, LOCK_EX|LOCK_NB) || die "$0 already running\n";
$BINDVAR = $ARGV[0];
$dbname = "COnnectionDetailsHere";
$un = "username";
$pw = "password";
my $dbh = DBI->connect ( $dbname, $un, $pw, { PrintError=>1, RaiseError=>1, AutoCommit=>0, LongReadLen=>5056}) or die "Couldn't connect to DB: " . DBI->errstr;
$sql = <<SQL
select * from foo where bar like '%?%' '?'
SQL
;
$stm = $dbh->prepare($sql) or die "SQL failed to prepare $DBI::errstr\n";
$stm->bind_param(1,($BINDVAR));
$stm->execute() or die "Couldn't execute the statement: " . $stm->errstr;