Once You Know, You Newegg

Perl mySQL Create Table

$result = $dbh->prepare(“CREATE TABLE `?` If NOT EXISTS ( `ID` INT( 255 ) NOT NULL AUTO_INCREMENT , `URL` VARCHAR( 255 ) NOT NULL , PRIMARY KEY ( `ID` )) ENGINE = MYISAM ;”);
$result->execute($host);
$result = $dbh->prepare(“INSERT INTO `?` (URL) VALUES (‘?’)”);
$result->execute($host, $href);
print “Host added: ” . $host . “\n”;


So here is a sub from one of my scripts showing this in use.

#!/usr/bin/perl -w
use strict;
use CGI;
use CGI ‘:standard’;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
print CGI::header(-expires => ‘-1d’);
use DBI;
$|=1;
my $us_phone_regex =’1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?’;
my $email_regex =’^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$’;
my $link_regex = ‘^(((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\’\\\+&%\$#\=~_\-]+))*$’;
##
sub makecitytable{
$city = shift;
$pagelink = shift;
$email = shift;
$phone= shift;
#######################################################
unless( $pagelink =~ m/$link_regex/ ){$pagelink=’failed’;}
#######################################################
unless( $phone =~ m/$us_phone_regex/ ){$phone=’none’;}
#######################################################
unless( $email =~ m/$email_regex/ ){$email=’none’;}
#######################################################
############# I create a table based on a city name $city. Then populate it with all the links found
my ($moe,$larry,$curly) = hookup();
my $hookup = DBI->connect($moe,$larry,$curly) or die “$DBI::errstr”;
### if I wanted unique email or whatever:
### $result = “CREATE TABLE IF NOT EXISTS `$city` (`ad_page` VARCHAR(255),PRIMARY KEY(ad_page),`email` VARCHAR(255),`phone` VARCHAR(26),UNIQUE KEY `email` (`email`))”;
### end unique example
$result = “CREATE TABLE IF NOT EXISTS `$city` (`ad_page` VARCHAR(255),PRIMARY KEY(ad_page),`email` VARCHAR(255),`phone` VARCHAR(26))”;
$result = $hookup->prepare($result);
$result->execute() or die “SQL Error: $DBI::errstr\n”;
#$result->execute($city) or die “Make table did not execute”;
$result = “INSERT INTO `$city` (ad_page,email,phone) VALUES (?,?,?) ON DUPLICATE KEY UPDATE `email`=values(email), `phone`=values(phone)” or die “Prepare insert did not execute”;
$result = $hookup->prepare($result);
$result->execute($pagelink,$email,$phone) or die “Insert email and phone did not execute”;
print qq~ added: pagelink=$pagelink email=$email and phone=$phone into $city
~;
$result->finish;
$hookup->disconnect;
#exit;
}# END SUB
############### connect to the database ######################
sub hookup {
my $moe = ‘DBI:mysql:databasename:localhost:3306′;
my $larry = ‘username’;
my $curly = ‘password’;
return (“$moe”,”$larry”,”$curly”);
}

mySQL DBI Interface Calls

Perl DBI for MySQL

This section documents the Perl DBI interface
for MySQL.

DBI is a generic interface for many databases. That means that
you can write a script that works with many different database engines
without change. You need a DataBase Driver (DBD) defined for each
database type. For MySQL, this driver is called
DBD::mysql.

You can get man page information about DBI with these:
man DBI
man DBI::FAQ
man DBD::mysql

More DBI/DBD information

For more information on the Perl5 DBI, please visit the DBI web
page and read the documentation:

http://www.symbolstone.org/technology/perl/DBI/index.html

For more information on Object Oriented Programming
(OOP) as defined in Perl5, see the Perl OOP page:

http://language.perl.com/info/documentation.html

And of course you can find the latest DBI information at
the DBI web page:

http://www.symbolstone.org/technology/perl/DBI/index.html

The DBI interface
Portable DBI methods




































connect Establishes a connection to a database server
disconnect Disconnects from the database server
prepare Prepares a SQL statement for execution
execute Executes prepared statements
do Prepares and executes a SQL statement
quote Quotes string or BLOB values to be inserted
fetchrow_array Fetches the next row as an array of fields.
fetchrow_arrayref Fetches next row as a reference array of fields
fetchrow_hashref Fetches next row as a reference to a hashtable
fetchall_arrayref Fetches all data as an array of arrays
finish Finishes a statement and let the system free resources
rows Returns the number of rows affected
data_sources Returns an array of databases available on localhost
ChopBlanks Controls whether fetchrow_* methods trim spaces
NUM_OF_PARAMS The number of placeholders in the prepared statement
NULLABLE Which columns can be NULL
trace Perform tracing for debugging

MySQL-specific methods


























insertid The latest AUTO_INCREMENT value
is_blob Which column are BLOB values
is_key Which columns are keys
is_num Which columns are numeric
is_pri_key Which columns are primary keys
is_not_null Which columns CANNOT be NULL. See NULLABLE.
length Maximum possible column sizes
max_length Maximum column sizes actually present in result
NAME Column names
NUM_OF_FIELDS Number of fields returned
table Table names in returned set
type All column types

The Perl methods are described in more detail in the following sections.
Variables used for method return values have these meanings:


$dbh

Database handle

$sth

Statement handle

$rc

Return code (often a status)

$rv

Return value (often a row count)


Portable DBI methods


connect($data_source, $username, $password)

Use the connect method to make a database connection to the data
source. The $data_source value should begin with
DBI:driver_name:.
Example uses of connect with the DBD::mysql driver:

$dbh = DBI->connect(“DBI:mysql:$database”, $user, $password);
$dbh = DBI->connect(“DBI:mysql:$database:$hostname”,
$user, $password);
$dbh = DBI->connect(“DBI:mysql:$database:$hostname:$port”,
$user, $password);

If the user name and/or password are undefined, DBI uses the
values of the DBI_USER and DBI_PASS
environment variables,
respectively. If you don’t specify a hostname, it defaults to
‘localhost’. If you don’t specify a port number, it defaults to the
default MySQL port (3306).

As of Msql-Mysql-modules version 1.2009,
the $data_source value allows certain modifiers:

mysql_read_default_file=file_name

Read `filename’ as an option file.

mysql_read_default_group=group_name

The default group when reading an option file is normally the
[client] group.
By specifying the mysql_read_default_group
option, the default group becomes the [group_name] group.

mysql_compression=1

Use compressed communication between the client and server.

mysql_socket=/path/to/socket

Specify the pathname of the Unix socket that is used to connect to the server.


Multiple modifiers may be given; each must be preceded by a semicolon.

For example, if you want to avoid hardcoding the user name and password into
a DBI script, you can take them from the user’s `~/.my.cnf’
option file instead by writing your connect call like this:

$dbh = DBI->connect(“DBI:mysql:$database”
. “;mysql_read_default_file=$ENV{HOME}/.my.cnf”,
$user, $password);

This call will read options defined for the [client] group in the
option file. If you wanted to do the same thing, but use options specified
for the [perl] group as well, you could use this:

$dbh = DBI->connect(“DBI:mysql:$database”
. “;mysql_read_default_file=$ENV{HOME}/.my.cnf”
. “;mysql_read_default_group=perl”,
$user, $password);

disconnect

The disconnect method disconnects the database
handle from the database.
This is typically called right before you exit from the program.
Example:

$rc = $dbh->disconnect;

prepare($statement)

Prepares a SQL statement for execution by the database engine
and returns a statement handle ($sth) which you can use to invoke
the execute method.
Typically you handle SELECT statements
(and SELECT-like statements
such as SHOW, DESCRIBE and EXPLAIN)
by means of
prepare and execute.
Example:

$sth = $dbh->prepare($statement)
or die “Can’t prepare $statement: $dbh->errstr\n”;



execute

The execute method executes a prepared statement. For
non-SELECT statements, execute returns
the number of rows
affected. I
f no rows are affected, execute returns “0E0”,
which Perl treats as zero but regards as true.
For SELECT statements,
execute only starts the SQL query in the database;
you need to use one
of the fetch_* methods described below to retrieve the data.
Example:

$rv = $sth->execute
or die “can’t execute the query: $sth->errstr;

do($statement)

The do method prepares and executes a SQL statement and returns the
number of rows affected. If no rows are affected, do returns
“0E0”, which Perl treats as zero but regards as true.
This method is
generally used for non-SELECT statements which cannot be prepared in
advance (due to driver limitations) or which do not need to executed more
than once (inserts, deletes, etc.). Example:

$rv = $dbh->do($statement)
or die “Can’t execute $statement: $dbh- >errstr\n”;

quote($string)

The quote method is used to “escape”
any special characters contained in
the string and to add the required outer quotation marks.
Example:

$sql = $dbh->quote($string)

fetchrow_array

This method fetches the next row of data and returns it as an array of
field values. Example:

while(@row = $sth->fetchrow_array) {
print qw($row[0]\t$row[1]\t$row[2]\n);
}

fetchrow_arrayref

This method fetches the next row of data and returns it as a reference
to an array of field values. Example:

while($row_ref = $sth->fetchrow_arrayref) {
print qw($row_ref->[0]\t$row_ref->[1]\t$row_ref->[2]\n);
}

fetchrow_hashref

This method fetches a row of data and returns a reference to a hash
table containing field name/value pairs. This method is not nearly as
efficient as using array references as demonstrated above. Example:

while($hash_ref = $sth->fetchrow_hashref) {
print qw($hash_ref->{firstname}\t$hash_ref->{lastname}\t\
$hash_ref- > title}\n);
}

fetchall_arrayref

This method is used to get all the data (rows) to be returned from the
SQL statement. It returns a reference to an array of references to arrays
for each row. You access or print the data by using a nested
loop. Example:

my $table = $sth->fetchall_arrayref
or die “$sth->errstr\n”;
my($i, $j);
for $i ( 0 .. $#{$table} ) {
for $j ( 0 .. $#{$table->[$i]} ) {
print “$table->[$i][$j]\t”;
}
print “\n”;
}

finish

Indicates that no more data will be fetched from this statement
handle. You call this method to free up the statement handle and any
system resources associated with it. Example:

$rc = $sth->finish;

rows

Returns the number of rows changed (updated, deleted, etc.) by the last
command. This is usually used after a non-SELECT
execute
statement. Example:

$rv = $sth->rows;

DT>NULLABLE


Returns a reference to an array of boolean values; for each element of
the array, a value of TRUE indicates that this
column may contain NULL values.
Example:

$null_possible = $sth->{NULLABLE};

NUM_OF_FIELDS

This attribute indicates
the number of fields returned by a SELECT
or SHOW FIELDS
statement. You may use this for checking whether a statement returned a
result: A zero value indicates a non-SELECT statement like
INSERT, DELETE or UPDATE.
Example:

$nr_of_fields = $sth->{NUM_OF_FIELDS};

data_sources($driver_name)

This method returns an array containing names of databases available to the
MySQL server on the host ‘localhost’.
Example:

@dbs = DBI->data_sources(“mysql”);

ChopBlanks

This attribute determines whether the fetchrow_* methods will chop
leading and trailing blanks from the returned values.
Example:

$sth->{‘ChopBlanks’} =1;

trace($trace_level)

trace($trace_level, $trace_filename)

The trace method enables or disables tracing. When invoked as a
DBI class method, it affects tracing for all handles.
When invoked as
a database or statement handle method, it affects tracing for the given
handle (and any future children of the handle).
Setting $trace_level
to 2 provides detailed trace information.
Setting $trace_level to 0
disables tracing. Trace output goes to the standard error output by
default. If $trace_filename is specified, the file is opened in
append mode and output for all traced handles is written to that
file. Example:

DBI->trace(2); # trace everything
DBI->trace(2,”/tmp/dbi.out”); # trace everything to /tmp/dbi.out
$dth->trace(2); # trace this database handle
$sth->trace(2); # trace this statement handle

You can also enable DBI
tracing by setting the DBI_TRACE
environment variable. Setting it to a numeric value is equivalent to calling
DBI->(value).
Setting it to a pathname is equivalent to calling
DBI->(2,value).



MySQL-specific methods


The methods shown below are MySQL-specific and not part of the
DBI standard. Several of them are now deprecated:
is_blob, is_key, is_num,
is_pri_key,
is_not_null, length,
max_length, and table.
Where DBI-standard alternatives exist, they are noted below.



insertid

If you use the AUTO_INCREMENT feature of MySQL,
the new
auto-incremented values will be stored here.
Example:

$new_id = $sth->{insertid};

As an alternative, you can use $dbh->{‘mysql_insertid’}.

is_blob

Returns a reference to an array of boolean values; for each element of the
array, a value of TRUE indicates that the
respective column is a BLOB.
Example:

$keys = $sth->{is_blob};

is_key

Returns a reference to an array of boolean values; for each element of the
array, a value of TRUE indicates that the
respective column is a key.
Example:

$keys = $sth->{is_key};

is_num

Returns a reference to an array of boolean values; for each element of the
array, a value of TRUE indicates that the
respective column contains numeric values.
Example:

$nums = $sth->{is_num};

is_pri_key

Returns a reference to an array of boolean values; for each element of the
array, a value of TRUE indicates that the respective column is a primary key.
Example:

$pri_keys = $sth->{is_pri_key};

is_not_null

Returns a reference to an array of boolean values; for each element of the
array, a value of FALSE indicates that this column may contain NULL
values.
Example:

$not_nulls = $sth->{is_not_null};

is_not_null is deprecated; it is preferable to use the
NULLABLE attribute (described above), since that is a DBI standard.

length

max_length

Each of these methods returns a reference to an array of column sizes. The
length array indicates the maximum possible sizes that each column may
be (as declared in the table description). The max_length array
indicates the maximum sizes actually present in the result table. Example:

$lengths = $sth->{length};
$max_lengths = $sth->{max_length};

NAME

Returns a reference to an array of column names.
Example:

$names = $sth->{NAME};

table

Returns a reference to an array of table names.
Example:
$tables = $sth->{table};
type

Returns a reference to an array of column types.
Example:
$types = $sth->{type};


This tutorial shows how to use Perl to retrieve rows from a MySQL table using Perl DBI.

You require these:

Perl DBD::MySQL module
MySQL server

If you haven’t got DBD::MySQL installed, you can install it with this:

sudo perl -MCPAN -e “install DBD::MySQL”

Then, test if DBD::MySQL is correctly installed with this:

perl -MDBD::MySQL -e “print 1234”

If it prints 1234, you know it is installed.
Create table “domains”

Create this table by running this SQL command in MySQL:

CREATE TABLE IF NOT EXISTS `domains` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`url` varchar(100) NOT NULL,
`descr` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=51 ;

Let’s populate this table by executing this INSERT command in MySQL. This will insert 50 rows of domain names, URLs and descriptions.
INSERT INTO `domains` (`id`, `name`, `url`, `descr`) VALUES (1, ’99designs’, ‘www.99designs.com/’, ‘Graphic Design’), (2, ‘AddictLab’, ‘www.addictlab.com/index.php/Home’, ‘Advertising, General’), (3, ‘Article One Partners’, ‘www.articleonepartners.com/’, ‘U.S. Patent Reform / Prior Art Research’), (4, ‘ArtistShare’, ‘www.artistshare.com/home/default.aspx’, ‘Music’), (5, ‘Battle Of Concepts’, ‘battleofconcepts.nl/’, ‘Advertising Creative’), (6, ‘Blellow’, ‘www.blellow.com/’, ‘General’), (7, ‘Bon Bon Kakku’, ‘bonbonkakku.com/’, ‘Graphic Design’), (8, ‘BootB’, ‘www.bootb.com/en/’, ‘Advertising, Creative industries’), (9, ‘Brewtopia’, ‘brewtopia.com.au/about-brewtopia.php’, ‘Brewery’), (10, ‘Brownbook’, ‘www.brownbook.net/’, ‘Business, General’), (11, ‘Cafe Press’, ‘www.cafepress.com/’, ‘Accessories, Clothes, Shopping’), (12, ‘ambrian House’, ‘www.cambrianhouse.com/’, ‘General’), (13, ‘Cameesa’, ‘cameesa.com/’, ‘Apparel / Design’), (14, ‘Catwalk Genius’, ‘www.catwalkgenius.com/default.asp’, ‘Fashion / Design / Shopping’), (15, ‘College Prowler’, ‘collegeprowler.com/’, ‘Education’), (16, ‘Colspark’, ‘www.colspark.com/’, ‘Business / Sales / Marketing’), (17, ‘ConsultingCrowd’, ‘consultingcrowd.com/’, ‘Business, general’), (18, ‘Covestor’, ‘www.covestor.com/’, ‘Investing’), (19, ‘Creatad’, ‘www.creatad.com/en-US/watch.aspx’, ‘Advertising’), (20, ‘Create My Tattoo’, ‘www.createmytattoo.com/altD’, ‘Fashion, Design’), (21, ‘Crowdsound’, ‘www.crowdsound.com/’, ‘Customer feedback’), (22, ‘Crowdspirit’, ‘www.crowdspirit.com/’, ‘General’), (23, ‘Crowdspring’, ‘www.crowdspring.com/’, ‘Design’), (24, ‘CureTogether’, ‘www.curetogether.com/’, ‘Medicine’), (25, ‘Current’, ‘current.com/’, ‘Film/TV’), (26, ‘Data Discoverers’, ‘www.datadiscoverers.com/’, ‘Data’), (27, ‘DoNanza’, ‘www.donanza.com/’, ‘Agrregates All crowdsourcing projects from across the web into one place.’), (28, ‘Dream Heels’, ‘www.dreamheels.com/’, ‘Printed High Heels / Graphic Design / Shopping / Footwear’), (29, ‘Ebbsfleet FC’, ‘www.myfootballclub.co.uk/’, ‘Sports’), (30, ‘Ekotekoo’, ‘projets.ekotekoo.fr/’, ‘Cleantech’), (31, ‘Elastic Lab’, ‘www.elasticlab.com/’, ‘Advertising, Video Production’), (32, ‘Exuve’, ‘www.exuve.com/’, ‘Apparel’), (33, ‘EyeKa’, ‘en.eyeka.com/corporate/offers’, ‘Marketing’), (34, ‘Fat Muffin’, ‘fatmuffin.com/’, ‘Advertising’), (35, ‘FeVote’, ‘www.fevote.com/’, ‘General’), (36, ‘Featurelist’, ‘featurelist.org/’, ‘IT/Computers/Internet’), (37, ‘Feedback2.0’, ‘www.feedback20.com/’, ‘Ideas+Answers’), (38, ‘Fellowforce’, ‘www.fellowforce.com/’, ‘General’), (39, ‘Fiskateers’, ‘www.fiskateers.com/’, ‘Arts/Crafts’), (40, ‘Foldit’, ‘fold.it/portal/’, ‘Science’), (41, ‘Freerisk’, ‘freerisk.org/’, ‘Finance’), (42, ‘Galaxy Zoo’, ‘https://www.galaxyzoo.org/’, ‘Aerospace’), (43, ‘GeniusRocket’, ‘www.geniusrocket.com/’, ‘Video, Design, and Copywriting’), (44, ‘Get Satisfaction’, ‘getsatisfaction.com/’, ‘Customer Service’), (45, ‘Global Lives’, ‘globallives.org/’, ‘Film/TV’), (46, ‘Goosegrade’, ‘www.goosegrade.com/’, ‘Editing’), (47, ‘Graniph’, ‘www.graniph.com/’, ‘Apparel’), (48, ‘Guardian (Tech News on Sun Oracle)’, ‘spreadsheets.google.com/ccc?key=pzqNmTm9PsGwLdHQvGrCPLw’, ‘News’), (49, ‘Halfbakery’, ‘www.halfbakery.com/’, ‘Ideas?’), (50, ‘Zazzle’, ‘www.zazzle.com/’, ‘Accessories, Clothes, General, Shopping’);

Save this script as mysqlselect.pl and run it:

#!/usr/bin/perl -w

use strict;
use DBI;

# Variables
my $str = shift || die “– Please enter a search key for domain name\n”; # search string
my $username = ‘xxxx’; # set your MySQL username
my $password = ‘****’; # set your MySQL password
my $database = ‘dddd’; # set your MySQL database name
my $server = ‘localhost’; # set your server hostname (probably localhost)

# Remove end-of-line from input
chomp $str;

# Get the rows from database
my $dbh = DBI->connect(“DBI:mysql:$database;host=$server”, $username, $password)
|| die “Could not connect to database: $DBI::errstr”;
my $sth = $dbh->prepare(‘select name, url from domains where name like ?’)
|| die “$DBI::errstr”;
$sth->bind_param(1, “%$str%”);
$sth->execute();

# Print number of rows found
if ($sth->rows < 0) { print "Sorry, no domains found.\n"; } else { printf ">> Found %d domains\n”, $sth->rows;
# Loop if results found
while (my $results = $sth->fetchrow_hashref) {
my $domainname = $results->{name}; # get the domain name field
my $url = $results->{url}; # get the URL field
printf ” +— \e[1;42m %s (%s)\e[0m\n”, $domainname, $url;
}
}

# Disconnect
$sth->finish;
$dbh->disconnect;

mySQL get data into arrays

Okay, say you have a database with some banner data. This was used for a PHP script but, I have not tested in Perl.
The placement there is a column `placement` which could contain data like sideadtop, sideadmiddle, sideadbottom or any position info.
Below will get each found `placement` into its’ own array.

$result = @mysql_query(“SELECT url, image, placement FROM advert WHERE user=’1′ ORDER BY placement”);
$i = 0;
while($process = @mysql_fetch_assoc($result)){
$row[$i] = $process;
$placement = $row[$1][‘placement’];
$$placement[‘url’] = $row[$1][‘url’];
$$placement[‘image’] = $row[$1][‘image’];
$i++;
}

so now we have the three arrays
$sideadtop,$sideadmiddle,$sideadbottom
with contents url and image
sorted by placement alphabetically ascending


Here is another way to get data into rows:
############################### get lines into individual arrays ###################################
#This approach stores the results of a SQL call in an array. In this example,
#the query is calling for rows with three columns of data, and printing each
#row

use DBI;

$dbh = DBI->connect(‘DBI:mysql:[DATABASE]‘, ‘[USER]‘, ‘[PASSWORD]‘)
|| die “ERROR: $DBI::errstr”;

$query = “[SQL QUERY GOES HERE]“;

$sth = $dbh->prepare($query);

$sth->execute();

$data = $sth->fetchall_arrayref();
$sth->finish;

foreach $data ( @$data) {

($variable1, $variable2, $variable3) = @$data;

print “$variable1\n”;
print “$variable2\n”;
print “$variable3\n”;

}

$dbh->disconnect(