Solved

Using Perl to create mySQL database?

Posted on 2004-03-29
9
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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.

728 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