Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I create tables with Perl's DBI?

Perl DBI

How do I create tables with Perl's DBI?

by  mbaranski  Posted    (Edited  )
This is a pretty simple example of how to create a table using mysql, if you know sql, and are using another database, the syntax may be a little bit different to connect, but otherwise this should work.

#!/usr/bin/perl

########################################################################
# init.pl #
#This file creates all of the tables, but does not set any perms or #
#add any records. It simply sets up the database to be filled. #
#The database is called killer, and it connects w/ no uname or passwd #
#Last modified on 12-4-2000 by Mike Baranski #
########################################################################
#If you know C/C++, the following 3 statements are roughly equivalent
#to #include <theFile.h> statements, they "import" functions.

use DBI; #The database Independent Driver, see Perldoc DBI
use DBD::mysql; #This particular database, mysql for this case
use strict; #require strict pragmas, not really needed
#but it keeps us honest!

#######################################################################
#The max length for a varchar, it may change... #
#The reason we do this is so that if the length does change, you only #
#have to change one value to upadate all of the VARCHARS, instead of #
#looking through evey line of code #
#######################################################################
my $max_varchar = 255;

my $database_name = "killer"; #This is the database we're creating

#######################################################################
#The following lines actually create a live handle to the database, #
#which is what dbh stands for (DataBase Handle) #
#######################################################################
my $dsn = "DBI:mysql:database=test;host=mike";
my $dbh = DBI->connect($dsn, undef, undef);

#######################################################################
#This is how to execute SQL statements on a database, this one drops #
#the database, be sure you want to! #
#######################################################################
my $sth = $dbh->prepare("DROP DATABASE IF EXISTS killer");
$sth->execute();

########################################################################
#More sql statements, it's pretty self-explanatory #
########################################################################
$sth = $dbh->prepare("CREATE DATABASE killer");
$sth->execute();

########################################################################
#Create a table called problems #
########################################################################
$sth = $dbh->prepare("CREATE TABLE $database_name.problems (problem_number INT NOT NULL,
PRIMARY KEY (problem_number), company INT, category
VARCHAR($max_varchar),
description_filename VARCHAR($max_varchar),
closed_by INT, open_or_closed INT, entered DATE,
closed DATE, urgency INT,
contact_person VARCHAR($max_varchar),
labor_time INT,
parts_sold VARCHAR($max_varchar)
)");
$sth->execute();
########################################################################
#Create the Company Table #
########################################################################
$sth = $dbh->prepare("CREATE TABLE $database_name.company (company_number INT NOT NULL,
PRIMARY KEY (company_number), address1 VARCHAR($max_varchar),
address2 VARCHAR($max_varchar), address3 VARCHAR($max_varchar),
main_contact VARCHAR($max_varchar), sec_contact VARCHAR($max_varchar),
phone_number VARCHAR($max_varchar), abbreviation VARCHAR(20),
contract_type INT, customer_since DATE, number_of_employees INT,
primary_technician INT, secondary_technician INT)");

$sth->execute();
########################################################################
#Create the Employee Table #
########################################################################
$sth = $dbh->prepare("CREATE TABLE $database_name.employee (employee_number INT NOT NULL,
PRIMARY KEY (employee_number), address1 VARCHAR($max_varchar),
address2 VARCHAR($max_varchar), address3 VARCHAR($max_varchar),
phone VARCHAR(15), cell VARCHAR(15), pager VARCHAR(15),
email VARCHAR($max_varchar), email_pager VARCHAR($max_varchar),
birthday DATE, pay_rate DOUBLE)");
$sth->execute();

#######################################################################
#This properly releases the handles #
#######################################################################
$sth->finish();
$dbh->disconnect();
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top