Solved

Using Perl to create mySQL database?

Posted on 2004-03-29
9
155 Views
Last Modified: 2013-12-25
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()?
0
Comment
Question by:BobbyAne2929839149
9 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10713132
use DBI;
my $dbh = new DBI::connect('dbi:mysql:database=your_db;host:localhost','dbuser','dbpass');
my $sth = $dbh->prepare('select * from yourtable');
   $sth->execute();
   while( --your-condition-and-tool-here--) {
   }
   $dbh->disconnect;
0
 

Author Comment

by:BobbyAne2929839149
ID: 10716969
How does that created the coloumns for the table? Wouldn't that just get data?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10721060
it's an example how to use perls DBI module, not a lesson on (My)SQL
anyway:
  my $sth = $dbh->prepare("insert into yourtable set col1='col1'");
     $sth->execute();
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:BobbyAne2929839149
ID: 10721313
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.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10723010
hmm, are you asking about SQL statements?
0
 
LVL 9

Accepted Solution

by:
ronan_40060 earned 125 total points
ID: 10768627
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 (
                   id    INTEGER NOT NULL  AUTO_INCREMENT PRIMARY KEY,
                  name     VARCHAR(32)  NOT NULL,
                 website   VARCHAR(32)  ,
                  description  VARCHAR(32),
                 email           VARCHAR(32) NOT NULL)");

$sth->execute();
$sth->finish();
print "Success\n";
$dbh->disconnect || die "Failed to disconnect \n";


Let me know if your unclear

 
0
 
LVL 2

Assisted Solution

by:Fataqui
Fataqui earned 125 total points
ID: 10768632
Hi


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)

change...

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))");

$sth->execute;

$sth->finish;

$db->disconnect();

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


F!

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question