Using Perl to create mySQL database?

I went to my CPanel and created a mySQL database. I have a lot of ideas I want to do, but they all require a database, so I figured I should learn. I bought the book Programming the Perl DBI, but it isn't very good. So I got my database created with CPanel, but now how do I use it? I have a user who has all access privelage, but I don't have any columns yet. CPanel gives me the connect string I need, would I just make some type of SQL statement, and do a do() then execute()?
Who is Participating?
ronan_40060Connect With a Mentor Commented:
here you go

use warnings;
use strict;
use DBI;

my ($dbh, $sth);

$dbh=DBI->connect('dbi:mysql:' ','user ','pass') || die " Error opening database : $DBI::errstr\n";

$sth=$dbh->prepare(" CREATE TABLE  test (
                  name     VARCHAR(32)  NOT NULL,
                 website   VARCHAR(32)  ,
                  description  VARCHAR(32),
                 email           VARCHAR(32) NOT NULL)");

print "Success\n";
$dbh->disconnect || die "Failed to disconnect \n";

Let me know if your unclear

use DBI;
my $dbh = new DBI::connect('dbi:mysql:database=your_db;host:localhost','dbuser','dbpass');
my $sth = $dbh->prepare('select * from yourtable');
   while( --your-condition-and-tool-here--) {
BobbyAne2929839149Author Commented:
How does that created the coloumns for the table? Wouldn't that just get data?
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

it's an example how to use perls DBI module, not a lesson on (My)SQL
  my $sth = $dbh->prepare("insert into yourtable set col1='col1'");
BobbyAne2929839149Author Commented:
I just created an empty table, no columns or anything. I need to know how to get an ID column that cannot be null and auto increments. Then I need a name column, website column, description column, and email column. After that, I think I can insert and select the data, but I just know how to create the columns.
hmm, are you asking about SQL statements?
FataquiConnect With a Mentor Commented:

Here you go....

simple example

Just change "TEST_TABLE" to what you want to call the table! Then set the values in $dbs to match your server $var(s)


database_name to your database name
database_user to your database user name
database_password to your database password

You should not need to change....

localhost or 3306

use DBI;
use strict;

my $dbs = 'localhost;3306;database_name;database_user;database_password';

my @ms_db = split(/;/, $dbs);

my $db = &make_connection;

my $sth = $db->prepare("CREATE TABLE TEST_TABLE(ID INT(10) NOT NULL auto_increment,
      name VARCHAR(60) default NULL,
      website VARCHAR(255) default NULL,
      description text,
      email VARCHAR(255) default NULL,
      PRIMARY KEY (ID))");




sub make_connection {
return DBI->connect("dbi:mysql:" . $ms_db[2] . ":" . $ms_db[0] . ":" . $ms_db[1], $ms_db[3], $ms_db[4]);


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.