我正在尝试使用 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 execute failed: ORA-00911: invalid character (DBD ERROR: error possibly near <> indicator at char 86 in 'delete from Employee where name = :p1 and id = :p2<>;') [for Statement "delete from Employee where name = ? and id = ?;" with ParamValues: :p1='3453', :p2='4545'] at ExecuteDelete.pl line 32, chunk 1.
Can't execute at ExecuteDelete.pl line 32, chunk 1
请帮帮我。我不确定我在这里是否做错了什么。
我已经尝试了所有其他方法..我给出了什么..一些如何“<*> ” - 这个特殊字符被附加..即使我尝试如下 请帮助我
我的 $sqlStatement = qq/delete from $tableName where name = ?和 ID = ?和地点 = $place/;
打印“声明是”。 $sql语句 . “\n”; 我的$sth = $dbh->准备($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']
最佳答案
您正在使用旧的方式打开文件。这是一种现代方法:
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;
同时删除“;”在你的 SQL 中
我推荐使用这个CPAN模块Data::Phrasebook::SQL ,它可能对您使用 SQL 的上下文有所帮助。
关于perl - 如何删除从perl读取sql文件时附加的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17556453/