Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating mysql database from perl

Status
Not open for further replies.

martinasv

Technical User
Feb 12, 2006
24
0
0
HR
Hi, everyone!

Is there a way to create a database using Perl?

I've been searching the Net, and all examples I came across were for connecting to an existing database.

I tried using: $result=$dbh->createdb("dbname"),
but it returns an error: Can't call method createdb on an undefined value...

I'd like my script to check if a certain database exists in MySQL, and if not, to create it.
 
Administration

shutdown, createdb, dropdb, reloadacls are all accessible via a database handle and implement the corresponding methods to what msqladmin does.

The mSQL and MySQL engines do not permit that these commands are invoked by users without sufficient privileges. So please make sure to check the return and error code when you issue one of them.

$rc = $dbh->shutdown();
$rc = $dbh->createdb($database);
$rc = $dbh->dropdb($database);

It should be noted that database deletion is not prompted for in any way. Nor is it undo-able from within Perl.

B<Once you issue the dropdb() method, the database will be gone!>

These methods should be used at your own risk.
 
I've managed to do it this way:

use DBI;

$username = 'root';$password = '';$database = 'tina'; $hostname = '';
$dbh = DBI->connect("dbi:mysql:database=$database;" .
"host=$hostname;port=3306", $username, ) or $result=`mysqladmin -u root create tina`;

Basically, it's doing what I wanted, but on top of creating a database if it doesn't already exist, it still prints out the error:
DBI connect('database=tina;host=;port=3306','root',...) failed: Unknown database 'tina'

Is there a way I can avoid that error being printed out?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top