[英]How to pass parameters from Perl script to SQL script and execute it from Perl script?
I use Informix as database in Linux environment.我在 Linux 环境中使用 Informix 作为数据库。 I have a Perl script which should execute an SQL-script.
我有一个应该执行 SQL 脚本的 Perl 脚本。 Before execution, it should also pass all parameters to the SQL-script.
在执行之前,它还应该将所有参数传递给 SQL 脚本。
I can't figure out how to pass parameters to .sql script?我不知道如何将参数传递给 .sql 脚本? It also does run but I get following error.
它也确实运行,但我收到以下错误。
DBD::Informix::st fetchrow_array failed: SQL: -400: Fetch attempted on unopen cursor. at startSelectQuer.pl
How can I realize this?我怎么能意识到这一点?
selectQuer.sql选择查询语句
DROP TABLE IF EXISTS tempTable;
SELECT * FROM 'informix'.systables INTO TEMP tempTable;
DROP TABLE IF EXISTS magic_ant;
select * from del_new
where
id = $i_id and
year = $i_year and
month = $i_month
into
temp magic_ant;
DROP TABLE IF EXISTS magic_buck;
select * from upper_new
where
id = $i_id and
year = $i_year and
month = $i_month
into
temp magic_buck;
DROP TABLE IF EXISTS alleMagic;
select * from magic_ant
union
select * from magic_buck
into temp alleMagic;
select lname, fname, ext from alleMagic;
startSelectQuer.pl开始选择查询文件
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my ($ID) = $_[0];
my ($YEAR) = $_[1];
my ($MONTH) = $_[2];
my $BEG_ANT=801 ;
my $END_ANT=803 ;
my $BEG_BRU=802 ;
my $END_BRU=900 ;
my($dbh, $sth, $query);
######################################################################
my $database = "$ENV{DBNAME}";
my $user ="";
my $pass ="";
$dbh = DBI->connect("dbi:Informix:$database", $user, $pass);
######################################################################
die "failed to connect to MySQL database:DBI->errstr()" unless($dbh);
my $sqlFile = "/SQLSCRIPTS/selectQuer.sql";
open (SQL, "$sqlFile") or die("Can't open file $sqlFile for reading");
# Loop though the SQL file and execute each and every one.
while (my $line = <SQL>) {
chomp $line;
$line = join(' ',split(' ',$line));
if ((substr($line,0,2) ne '--') and (substr($line,0,3) ne 'REM')) {
if (substr($line,- 1,1) eq ';') {
$query .= ' ' . substr($line,0,length($line) -1);
# replace with value
replaceQueryWithValue($query);
$sth = $dbh->prepare($query, {'ix_CursorWithHold' => 1})
or die "prepare statement failed: $dbh->errstr()";
$sth->execute() or die "execution failed: $dbh->errstr()";
my $rows = $sth->rows;
#loop through each row of the result set, and print it.
if ($rows > 0) {
# Getting error here as: DBD::Informix::st fetchrow_array failed:
# SQL: -400: Fetch attempted on unopen cursor.
while(my @row = $sth->fetchrow_array) {
print qw($row[0]\t$row[1]\t$row[2]\n);
}
} else
{
print "\nThere is no result for query: $query\n" ;
}
$query = ' ';
} else {
$query .= ' ' . $line;
}
}
}
# close data connection
$sth->finish;
$dbh->disconnect;
sub replaceQueryWithValue{
$query =~ s/i_id/$ID/ig;
$query =~ s/i_year/$YEAR/ig;
$query =~ s/i_month/$MONTH/ig;
}
When asking a question like this, it's useful if you tell us exactly what isn't working as you expect it to.在提出这样的问题时,如果您确切地告诉我们哪些地方没有按照您的预期工作,这会很有用。 Does nothing happen?
什么都没发生? Do you get an error message?
您是否收到错误消息? Does your computer burst into flames?
你的电脑会起火吗?
Without that, we're pretty much guessing.没有那个,我们几乎是在猜测。 But I'm happy to have a guess here.
但我很高兴在这里有一个猜测。 I know nothing about Informix, but I would guess that you're seeing the "prepare statement failed" error.
我对 Informix 一无所知,但我猜您会看到“准备语句失败”错误。 Is that right?
那正确吗?
If so, it seems to be that you're trying to compile an SQL statement that looks like SQLSCRIPTS/selectQuer.sql
- when actually, that is the name of a file that you should be opening and reading your SQL statement from.如果是这样,您似乎正在尝试编译一个看起来像
SQLSCRIPTS/selectQuer.sql
的 SQL 语句 - 实际上,这是您应该打开并从中读取 SQL 语句的文件的名称。
I used $sth->fetch
instead of $sth->fetchrow_array
after execution of .sql as below.在执行 .sql 后,我使用
$sth->fetch
而不是$sth->fetchrow_array
,如下所示。
$sth = $dbh->prepare( "select * from alleMagic;" );
$sth->execute;
# Column binding is the most efficient way to fetch data
my $rv = $sth->bind_columns(\$lname, \$fname, \$ext );
while ($sth->fetch) {
print "$lname, $fname, $ext \n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.