Solved

Using Perl to create mySQL database?

Posted on 2004-03-29
9
153 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
Comment Utility
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
Comment Utility
How does that created the coloumns for the table? Wouldn't that just get data?
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:BobbyAne2929839149
Comment Utility
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
Comment Utility
hmm, are you asking about SQL statements?
0
 
LVL 9

Accepted Solution

by:
ronan_40060 earned 125 total points
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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

6 Experts available now in Live!

Get 1:1 Help Now