【发布时间】:2021-02-15 01:09:23
【问题描述】:
我在 Linux 环境中使用 Informix 作为数据库。 我有一个应该执行 SQL 脚本的 Perl 脚本。在执行之前,它还应该将所有参数传递给 SQL 脚本。
我不知道如何将参数传递给 .sql 脚本? 它也可以运行,但出现以下错误。
DBD::Informix::st fetchrow_array failed: SQL: -400: Fetch attempted on unopen cursor. at startSelectQuer.pl
我怎样才能意识到这一点?
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;
}
【问题讨论】:
-
尝试在字符串中插入对象函数调用可能不是一个好主意。
$dbh->errstr(). -
$sql在您的准备语句中应该是一个字符串,而不是文件地址。 -
在
replaceQueryWithValue($query);和$sth = $dbh->prepare($query, {'ix_CursorWithHold' => 1})两行之间,您应该安排打印$query中的值(适当注释),以便您可以看到您在询问Informix DBMS 准备然后执行。 'fetch on an unopen cursor' 表示您正在执行的任何语句都不是 select 语句 — 文件中的许多语句不是返回行的 select 语句(INTO TEMP子句意味着该语句不返回值) . -
注意
select * from del_new where id = $i_id and year = $i_year and month = $i_month and into temp magic_ant;等文件中的语句中的第三个and是错误的。您应该在第三个and之后包含另一个条件或删除它。 -
如果您使用的表包含最少的模式和最少的数据集,以及预期和实际结果,这将很有帮助。这是为 SQL 相关问题创建 MCVE(Minimal, Complete, Verifiable Example — 或 MRE 或 SO 现在使用的任何名称)或 SSCCE(Short, Self-Contained, Correct Example)的一部分。强迫那些可能帮助你的人做这项工作并不容易得到答案——一般来说,人们不会这样做。
标签: linux perl unix informix sql-scripts