database connect: protect with exceptions?

Discussion in 'Perl Misc' started by filippo, Sep 18, 2006.

  1. filippo

    filippo Guest

    Hello,

    hereafter is the command I use to connect my database;

    my $dbhCampeggio = DBI->connect(
    "DBI:pgPP:dbname=$DATABASE_NAME;host=$DATABASE_SERVER;port=5432",
    "postgres",
    "postgres",
    { RaiseError => 1}
    ) or die ( "Connessione al DATABASE non riuscita: $DBI::errstr");

    $DATABASE_SERVER is passed from command line. I want my program to ask
    interactively the user to write the name, If the user forget to pass
    the database server at command line, and the program ask the user
    whenever she doesn't write a valid database server.

    How can I do?

    Thanks
    Filippo
    filippo, Sep 18, 2006
    #1
    1. Advertising

  2. filippo <> wrote:

    > $DATABASE_SERVER is passed from command line. I want my program to ask
    > interactively the user to write the name, If the user forget to pass
    > the database server at command line,



    my $DATABASE_SERVER = shift @ARGV;
    unless ( defined $DATABASE_SERVER ) {
    print STDOUT 'enter the database server name: ';
    $DATABASE_SERVER = <STDIN>;
    chomp $DATABASE_SERVER;
    }


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 18, 2006
    #2
    1. Advertising

  3. filippo

    Filippo Guest

    Tad McClellan ha scritto:

    > my $DATABASE_SERVER = shift @ARGV;
    > unless ( defined $DATABASE_SERVER ) {
    > print STDOUT 'enter the database server name: ';
    > $DATABASE_SERVER = <STDIN>;
    > chomp $DATABASE_SERVER;
    > }


    thanks Tad but this isn't what I want: the unless clause accept any
    value for DATABASE_SERVER. I want to try to connect and get the value
    unless the connection is succeeded fine.

    Thanks and best regards,

    Filippo
    Filippo, Sep 19, 2006
    #3
  4. filippo

    J. Gleixner Guest

    Filippo wrote:
    > Tad McClellan ha scritto:
    >
    >> my $DATABASE_SERVER = shift @ARGV;
    >> unless ( defined $DATABASE_SERVER ) {
    >> print STDOUT 'enter the database server name: ';
    >> $DATABASE_SERVER = <STDIN>;
    >> chomp $DATABASE_SERVER;
    >> }

    >
    > thanks Tad but this isn't what I want: the unless clause accept any
    > value for DATABASE_SERVER. I want to try to connect and get the value
    > unless the connection is succeeded fine.


    Wrap it in another loop. Also see:

    perldoc -f eval


    use DBI;
    my $dbh;
    my $DATABASE_SERVER = shift @ARGV;

    unless ( $dbh )
    {

    #... insert unless loop, from above, to set DATABASE_SERVER

    eval {
    $dbh = DBI->connect( ... $DATABASE_SERVER,...,
    { RaiseError => 1 } );
    };
    if ( $@ )
    {
    print "Couldn't connect using $DATABASE_SERVER.\n";
    undef $DATABASE_SERVER;
    }
    }
    J. Gleixner, Sep 19, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chanmmn
    Replies:
    2
    Views:
    428
  2. Otis Mukinfus
    Replies:
    3
    Views:
    902
    Juan T. Llibre
    Jul 8, 2005
  3. Replies:
    3
    Views:
    40,162
    Roedy Green
    Nov 16, 2005
  4. Replies:
    2
    Views:
    268
  5. Mohsen Pahlevanzadeh

    Qt connect and first connect or unicode

    Mohsen Pahlevanzadeh, Sep 17, 2013, in forum: Python
    Replies:
    3
    Views:
    145
    Mohsen Pahlevanzadeh
    Sep 18, 2013
Loading...

Share This Page