Creating a Database
Before you can open a connection to a database with DBI, you
must create the database. DBI can’t do this step for you, although
your DBD might allow you to. For example, DBD::MySQL implements a
_CreateDB
function. Your DBD
might also support the func
method, which is used to call private (and often nonportable)
methods in the driver. You could use a one-liner like this to create
the database from the command line:
perl -MDBI -e '$db_name = q[database_name_here
]; \
$result = DBD::mysql::dr->func($db_name, '_CreateDB');'
If your DBD allows to you to create databases via the API, it’s likely that it will allow you to drop them, too:
perl -MDBI -e '$db_name = q[database_name_here
]; \
$result = DBD::mysql::dr->func($db_name, '_DropDB');'
Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.