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();
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.