0

I am trying to connect with database and perform some SQL queries by using this code, but every time it hangs.

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?

1 Answer 1

3

Reading the documentation for the DBI module would be a good start.

Your problem seems to be this line.

$sth = dbi->prepare($sqlStatement)

You're trying to call the prepare method on the class "dbi". 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).

To use a database from Perl you need to do these things:

1/ Load the DBI module (note, "DBI", not "dbi" - Perl is case sensitive).

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).

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

3/ You can then use this database handle to prepare SQL statements.

my $sth = $dbh->prepare($sqlStatement);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.