Solved

Using Perl to create mySQL database?

Posted on 2004-03-29
9
157 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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