2

I'm trying to create a database in Perl but it keeps trying to ask me for a database to use. Here's my code:

my @db_months = qw(JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC);

foreach my $db_month (@db_months)
{
  ## create db
  my $db_name = $db_month.$Year;
  my $dbh = DBI->connect($dns, $user, $password) or die "Unable to connect: $DBI::errstr\n"; 
  my $row = $dbh->do("CREATE DATABASE '".$db_name."' DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;"); 
  $dbh->disconnect(); 
}

Please help.

4
  • 3
    What does $dns contain? What error do you get? Commented Feb 17, 2011 at 7:51
  • 1
    In this context, a better nomenclature for $dns would be $dsn. Commented Feb 17, 2011 at 8:05
  • Agreed. In general, DNS = Domain Name System and DSN = Data Source Name. Commented Feb 17, 2011 at 8:54
  • As others have said, what line throws an error, and what is the error? Also, a table for every month? It sounds like you're doing database schemas all wrong, unless you have a very unusual requirement which makes that necessary. Commented Feb 17, 2011 at 11:02

3 Answers 3

2

Just simply omit the database=$database;:

$dbh = DBI->connect("DBI:mysql:host=$host;port=$port", $user, $pw);
$dbh->do("create database test") or die "Cannot create database \n ";
Sign up to request clarification or add additional context in comments.

Comments

1

It really helps to know which line is giving an error and what precisely the error is.

Try using backticks (`) instead of single quotes (') around the database name...

Comments

0

The server administration function of DBD::mysql can be used:

my $rc = $dbh->func( 'createdb', $db_name, 'admin' );

2 Comments

Can I specify collation with createdb? I haven't found it in DBD/mysql.pm
No, you can't. I'm really not a fan of this part of DBD::mysql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.