Last active
May 19, 2018 00:03
-
-
Save glwinsor/72f15c2bb98d977af21274599854d0a4 to your computer and use it in GitHub Desktop.
mysql db handle from command line params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use warnings; | |
use Getopt::Long; | |
use DBI; | |
my $database = ''; | |
my $password = ''; | |
my $user = ''; | |
my $host = ''; | |
my $port = 3306; | |
GetOptions( | |
'database=s' => \$database, | |
'user=s' => \$user, | |
'password=s' => \$password, | |
'host=s' => \$host, | |
'port=i' => \$port, | |
); | |
if ( $database eq "" ) { | |
die "Enter the database name using --database <database>\n"; | |
} | |
if ( $user eq "" ) { | |
die "Enter username using --user <user>\n"; | |
} | |
if ( $password eq "" ) { | |
die "Enter password using --password <password>\n"; | |
} | |
if ( $host eq "" ) { | |
die "Enter hostname using --host <hostname>\n"; | |
} | |
# Create the db handle | |
my $dsn = "dbi:mysql:database=" . $database . ";host=" . $host . ";port=". $port; | |
my $dbh = DBI->connect( $dsn, $user, $password ) or die DBI->errstr; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment