Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > database connect: protect with exceptions?

Reply
Thread Tools

database connect: protect with exceptions?

 
 
filippo
Guest
Posts: n/a
 
      09-18-2006
Hello,

hereafter is the command I use to connect my database;

my $dbhCampeggio = DBI->connect(
"DBIgPP:dbname=$DATABASE_NAME;host=$DATABASE_SER VER;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

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-18-2006
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
 
Reply With Quote
 
 
 
 
Filippo
Guest
Posts: n/a
 
      09-19-2006

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

 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      09-19-2006
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;
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Cokkies - protect/unprotect? John Firefox 1 09-13-2005 03:33 AM
Protect my class Libraries, can I? Cablito ASP .Net 4 09-27-2004 06:14 PM
Problems accessing a password protect database chanmmn ASP .Net 2 09-07-2004 06:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57