mattdope has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, does anyone know a simple test dbi program i could use to check if i can communicate with my database?

Replies are listed 'Best First'.
Re: dbi test program
by marto (Cardinal) on Jul 21, 2006 at 09:13 UTC
    mattdope,

    Please read How do I post a question effectively?, did you Super Search or look in the Tutorials section of this site before posting? In Tutorials there is a Database Programming section. Did you read the DBI documentation or the documentation for the database driver for your database (you have not indicated which database platform you are using). The documentation contains code which you can use to test database connectivity. For example if you were using MySQL:
    #!/usr/bin/perl use strict; use DBI(); # Connect to the database. my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost", "joe", "joe's password", {'RaiseError' => 1});
    This is taken from the DBD::mysql documentation.

    Martin
Re: dbi test program
by davorg (Chancellor) on Jul 21, 2006 at 09:08 UTC

    There are probably simple connectivity test programs within the test suite that is run when you install the DBD module. You could look at those and adapt them.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: dbi test program
by jZed (Prior) on Jul 21, 2006 at 17:28 UTC
    Here's a script that will die with an error if any of the parts of the database communication process fail. You'll need to change the values for $dsn, $user, etc. but everything else should work regardles of which DBD you are using.
    #!/usr/bin/perl -w use strict; use DBI; print "Loaded the DBI module ... ok\n"; my($dsn,$user,$password,$attributes,$table)= ( 'dbi:CSV:' , 'me' , 'myPassword' , { RaiseError=>1, PrintError=>0, AutoCommit=>1 } , 'myTable' ); my $dbh = DBI->connect( $dsn, $user, $password, $attributes ); print "Connected to database '$dsn' ... ok\n"; my $success = $dbh->ping; print "Connection to database '$dsn' is active ... ok\n" if $success; my $sth = $dbh->prepare( "SELECT * FROM $table WHERE 1=0" ); $sth->execute; $sth->finish; print "Accessed table '$table' ... ok\n"; $dbh->disconnect; print "Disconnected ... ok\n"; __END__