Solved

Using Perl to create mySQL database?

Posted on 2004-03-29
9
154 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Do you have experts in the field of AutoIt coding? 25 114
Need help deploying an MSI via silent install using PSEXEC 4 59
EXCHANGE 2007, SCRIPTS 67 75
home folder path for users 4 56
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

947 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now