简体   繁体   English

如何在perl脚本中调用SQL

[英]how to invoke SQL inside perl script

I am trying to connect with database and perform some SQL queries by using this code, but every time it hangs. 我正在尝试使用此代码连接数据库并执行一些SQL查询,但每次它挂起时。

my $connect_str = `/osp/local/etc/.oralgn $srv_name PSMF`;
my $sqlFile = "/osp/local/home/linus/amit/mytest.sql";
my ($abc, $cde)= split (/\@/ , $connect_str );
print "$abc";

$ORACLE_SID=SDDG00;
`export $ORACLE_SID`;

#chomp($abc);

#$abc=~ s/\s+$//;
`sqlplus $abc`;

open (SQL, "$sqlFile");


while (my $sqlStatement = <SQL>) {
   $sth = dbi->prepare($sqlStatement)
         or die (qq(Can't prepare $sqlStatement));

            $sth->execute()
                  or die qq(Can't execute $sqlStatement);
                  }

How do I invoke a SQL command inside Perl ? 如何在Perl调用SQL命令?

Reading the documentation for the DBI module would be a good start. 阅读DBI模块文档将是一个不错的开始。

Your problem seems to be this line. 您的问题似乎是这条线。

$sth = dbi->prepare($sqlStatement)

You're trying to call the prepare method on the class "dbi". 您试图在类“ dbi”上调用prepare方法。 But you don't have a class called "dbi" in your program (or, at least, I can't see one in the code you've shown us). 但是您的程序中没有名为“ dbi”的类(或者,至少,在您展示给我们的代码中我看不到)。

To use a database from Perl you need to do these things: 要使用Perl中的数据库,您需要执行以下操作:

1/ Load the DBI module (note, "DBI", not "dbi" - Perl is case sensitive). 1 /加载DBI模块(注意,“ DBI”而不是“ dbi”-Perl区分大小写)。

use DBI;

2/ Connect to the database and get a database handle (Read the DBD::Oracle documentation for more details on the arguments to the connect() method). 2 /连接到数据库并获取数据库句柄(有关connect()方法的参数的更多详细信息,请阅读DBD :: Oracle文档 )。

my $dbh = DBI->connect('dbi:Oracle:dbname', $user, $password);

3/ You can then use this database handle to prepare SQL statements. 3 /然后,您可以使用此数据库句柄准备SQL语句。

my $sth = $dbh->prepare($sqlStatement);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM