Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to give a database as an input in a perl script?

by amature (Initiate)
on May 31, 2012 at 04:11 UTC ( #973423=perlquestion: print w/replies, xml ) Need Help??
amature has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks!

I have written a perl script which reads a whole directory and takes all the files in a directory as input. Now instead of giving a directory as a input, i want to give a database as a input and it must read all the tables in the database like reading the files in a directory.

How can i do this please suggest me a way or an idea. I would be very grateful for the same.

Thanks ahead of time!

  • Comment on How to give a database as an input in a perl script?

Replies are listed 'Best First'.
Re: How to give a database as an input in a perl script?
by GrandFather (Sage) on May 31, 2012 at 04:28 UTC

    Something like:

    use strict; use warnings; use DBI; my $dbh = DBI->connect(...); my $tables = $dbh->selectall_arrayref('show tables'); print "$_->[0]\n" for @$tables;
    True laziness is hard work

      Instead of the (somewhat) database specific show tables, I prefer to offload that knowledge to the database driver. DBI has lots of Catalog Methods that return structured data about the database itself. Of course, some of it is implemented by issuing a show tables command.

      Generic version:
      use strict; use warnings; use DBI; my $dbh = DBI->connect(...); my @tables = $dbh->tables(undef, undef, undef, 'TABLE'); print "$_\n" for @tables;
      The tables method should work for all databases supported by DBI. Sending a show tables query is MySQL-specific - I know it doesn't work with SQLite databases and, from long-gone memory, I'm pretty sure PostgreSQL wouldn't like it either. Any other database engine, I can only say "seems unlikely".

      @GrandFather Thanks now i have got some kind of idea

Re: How to give a database as an input in a perl script?
by choroba (Chancellor) on May 31, 2012 at 07:33 UTC
    Crossposted at StackOverflow. It is considered polite to inform about crossposting so people not visiting both sites do not waste their time on a problem already solved at the alien site.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://973423]
Approved by GrandFather
help
Chatterbox?
[1nickt]: Corion using Moose will hardly reduce the number of dependencies or time to install DT !
[davido]: That's like wishing that your motorcycle came with a sherman tank as a trailer.
[Corion]: davido: No, more like my one sherman tank had a second sherman tank tied to its back ;)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2017-03-23 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (289 votes). Check out past polls.