2

我正在尝试使用 perl 从 sql 文件执行查询,但是当我尝试时,我看到在查询之间或查询末尾附加了一些特殊的字符。我不确定如何解决这个问题。

my $dbh = DBI->connect("DBI:Oracle:$dbconnectstring","$user","$password");
open FH, "< Queries.sql"; 
$sth = $dbh->prepare(<FH>)     or die ("Can't prepare  $sqlStatement");
$sth->execute($name,$id)      or die ("Can't execute $sqlStatement");

sql文件中的SQL语句:

delete from Employee where name = ? and id = ? ;

以下是我得到的错误:

DBD::Oracle::st 执行失败:ORA-00911:无效字符(DBD 错误:在“从员工中删除名称 = :p1 和 id = :p2< >;”中的字符 86 处的 < > 指示符附近可能出现错误)[对于语句“从名称 = ?和 id = ?的员工中删除;” 与 ParamValues: :p1='3453', :p2='4545'] 在 ExecuteDelete.pl 第 32 行,块 1。

无法在 ExecuteDelete.pl 第 32 行,块 1 处执行

请在这里帮助我。我不确定我在这里是否做错了什么。

我已经尝试了所有其他方式..我给了什么..一些如何“<*>” - 这个特殊字符正在附加..即使我尝试如下请帮助我

我的 $sqlStatement = qq/delete from $tableName where name = ? 和身份证=?和地点 = $地点/;

打印“声明是”。$sql语句。"\n"; 我的 $sth = $dbh->prepare($sqlStatement);

DBD::Oracle::st execute failed: ORA-00904: invalid identifier (DBD ERROR: error possibly near <*> indicator at char 101 in 'delete from EMPLOYEE where ORDER_NUMBER = :p1 AND ORDER_VERSION = :p2 AND place = <*>EMPLOYEE') [for Statement "delete from EMPLOYEE where name = ? AND id = ? AND place = EMPLOYEE" with ParamValues: :p1='34534', :p2='09']
4

1 回答 1

2

you are using an old way to open files. This is a modern approach:

use strict;
use warnings;
#...
open my $fh, '<', 'Queries.sql' or die $!; 
my $sql = <$fh>;
chomp $sql;
my $sth = $dbh->prepare($sql);
$sth->execute($name,$id);
#... 
close $fh;

also remove the ";" in your SQL

I recommend the use of this CPAN module Data::Phrasebook::SQL, it may be helpful for you in the context you are using SQLs.

于 2013-07-09T19:31:41.247 回答