0

How to print the names of tables of database using Perl scripting using unix. Connected to DB using DBI module.

I tried scripting using

     my $driver= "Oracle";
     my $dsn = "DBI:$driver:sid=as;host=asdsa";
     my $dbh = DBI->connect($dsn, "a", "b") || die( $DBI::errstr . "\n" );
     my $notables = $dbh->tables();
     print "No of tables : $notables" ;

Getting error:

Can't call method "tables" on an undefined value at hello.pl line 16.

Please help.

3
  • 1
    Your $dbh object is undefined. Are you sure you connected to the database? Pleae edit your question and include the part that does my $dbh = DBI->connect. Or better, include the full program. Also take a look at How to Ask to learn how to format code in your question. Commented Sep 29, 2017 at 9:02
  • yes connected to db .. updated the code. Commented Sep 29, 2017 at 10:17
  • Is this really the code that produces this error? It seems unlikely to me. A call to connect() will return either a database handle or undef. Returning undef would trigger your die() call, so you must be getting a defined database handle back from connect(). I suspect there is something else going in parts of the code that you have removed to simplify your example. Commented Sep 29, 2017 at 12:50

1 Answer 1

1

Looks like you not connected to DB.

Read DBI documentation and try something like this:

use DBI;
use Data::Dumper;

my $dbh = DBI->connect($data_source, $username, $password)
          or die $DBI::errstr;

my @names = $dbh->tables( $catalog, $schema, $table, $type );
print Dumper @names;

$dbh->tables; without args is deprecated

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.