Both Perl and MySQL are major pieces of open source software with a multitude of facilities available - yet you can use them together with just a tiny piece of software "glue" - the DBI and DBD::MySQL module pair from
the CPAN (comprehensive Perl archive network).
Once you've got Perl and MySQL installed, the mysqld running, and DBI available to you, inserting data is as easy as connecting to the database:
$cid = DBI -> connect ("DBI:mysql:forsale:192.168.200.66","trainee","abc123");
and running your insert:
$cid->do ("Insert into agents (agent,phone) values ('$ARGV[0]','$ARGV[1]')");
Selecting and displaying records needs a couple more lines of code, since you have to step through ("iterate through") the result set very much like you would read through a file - necessary because the result set may be too big to process all at once. After connecting, prepare and execute your SQL SELECT command:
$action = $cid->prepare("Select agent,phone from agents");
$action->execute();
and you can then step through your result set row by row:
while (@row = $action->fetchrow()) {
print "Call $row[1] to reach $row[0]\n";}
Link -
Complete source code of the example that I've quoted from.
We now cover interfacing Perl to databases such as MySQL, and Oracle and Sybase which work through the same mechanism, briefly on our
learning to program in Perl course if any of our delegates with to interface to a database. We also cover the subject in more detail on our
MySQL and
Perl for Larger Projects courses.
A caution - if you're writing a program such as the example I've quoted here that will be used by people other than yourself, you need to add a little extra code to "clean" the user's input - check that it's data that you really want in your database, and that it's not formatted like a piece of malicious SQL to run what's known as an "injection attack" (written 2006-03-13, updated 2006-06-09)
Associated topics are indexed under
P308 - Using SQL Databases from Perl [3099] Perl - database access - DBD, DBI and DBIx modules - (2010-12-22)
[2561] The future of MySQL - (2010-01-03)
[2381] Checking the database connection manually - (2009-08-28)
[1904] Ruby, Perl, Linux, MySQL - some training notes - (2008-11-23)
[1885] Hiding a MySQL database behind a web page - (2008-11-15)
[1224] Object Relation Mapping (ORM) - (2007-06-09)
[975] Answering ALL the delegate's Perl questions - (2006-12-09)
[515] MySQL - an FAQ - (2005-12-03)
S156 - Interfacing Applications to MySQL Databases [3455] MySQL, MySQLi, PDO or something else - how best to talk to databases from PHP - (2011-09-24)
[3447] Needle in a haystack - finding the web server overload - (2011-09-18)
[3035] How to display information from a database within a web page - (2010-11-07)
[2790] Joining a MySQL table from within a Python program - (2010-06-02)
[2745] Connecting Python to sqlite and MySQL databases - (2010-04-28)
[2263] Mysqldump fails as a cron job - a work around - (2009-06-30)
[1561] Uploading to a MySQL database through PHP - examples and common questions - (2008-03-02)
[1518] Downloading data for use in Excel (from PHP / MySQL) - (2008-01-25)
[1450] Easy selection of multiple SQL conditions from PHP - (2007-11-30)
[1381] Using a MySQL database to control mod_rewrite via PHP - (2007-10-06)
[723] Viewing images held in a MySQL database via PHP - (2006-05-17)
[663] Python to MySQL - (2006-03-31)
[104] mysql_connect or mysql_pconnect in PHP? - (2004-10-30)
Some other Articles
West Wilts Railway Users Group and trains to LacockChecking for MySQL errorsPHP - London course, Melksham Course, Evening courseLost CamelUsing a MySQL database from PerlIf it's Sunday, it must be BedwynHow similar are two wordsSimple but rugged form handling demoTraining Centre PicturesProgress bars and other dynamic reports