Hanson, Robert wrote:

>"How do I CREATE a database from within perl with MySQL?"
>
>I don't think you can... but you can run the mysql interface from the
>script.  Actually I think that you can pipe a list of commands to mysql. 
>
>Rob
>
If you can get a connection with proper permissions, all you need to do 
is use:

$dbh->do("CREATE DATABASE foo");

I noticed that you don't have to issue a new connect to switch to the 
new database.

$dbh->do("use foo");

I also discovered that you don't need to specify a database when connecting.

my $dbh = DBI->connect('dbh:mysql:','root','xxxxxxx');

Here's a quick example:

#!/usr/bin/perl
use strict;
use DBI;

my $dbh = DBI->connect('dbi:mysql:','root','xxxxxx');
$dbh->do("CREATE DATABASE foo");
$dbh->do("use foo");

$dbh->do("
    CREATE TABLE bar(
       id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
       value VARCHAR(23) NOT NULL DEFAULT 'Kallisti'
    )
");
$dbh->do("
    INSERT INTO bar VALUES()
");
my $sth = $dbh->prepare("SELECT value FROM bar");
$sth->execute();
while( my($val) = $sth->fetchrow_array() ){
    print "$val\n";
}
$dbh->do("DROP DATABASE foo");
$dbh->disconnect();

Happy hacking,

- Johnathan


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to