| |||||||||||
| |||||||||||
Using a MySQL database from Perl
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 06:07:20) Associated topics are indexed under P308 - Using SQL Databases from PerlS156 - Interfacing Applications to MySQL Databases
Some other Articles
West Wilts Railway Users Group and trains to LacockChecking for MySQL errors PHP - London course, Melksham Course, Evening course Lost Camel Using a MySQL database from Perl If it's Sunday, it must be Bedwyn How similar are two words Simple but rugged form handling demo Training Centre Pictures Progress bars and other dynamic reports 1689 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34 at 50 posts per pageThis is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price. Link to Ezine home page (for reading). Link to Blogging home page (to add comments). |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||