koti688 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I need a perl package which will perform basic DB operations like select , insert,update,delete operation MySql DB. i Have a Mysql DB which resides on my local machine.DB Deatils are : Localhost,MySql,DatabaseName:perltest, username:root,pwd:sierra I can able to do the following thing for selecting rows in Db.
#!/usr/bin/perl -w use DBI; $dbh = DBI->connect('dbi:mysql:perltest','root','sierra') or die "Connection Error: $DBI::errstr\n"; $sql = "select * from samples"; $sth = $dbh->prepare($sql); $sth->execute or die "SQL Error: $DBI::errstr\n"; while (@row = $sth->fetchrow_array) { print "@row\n"; }
But not able to do Insert , delet and update operations and made all the functions into a package.coul guyz help me in doing these things. I am a beginner to perl. expecting quick replys from u. You can mail me ur proceeding to this id koti688@gmail.com or koteswara.devarasetty@sierraatlantic.com Thanks in Advance Koti.
  • Comment on Need Sample Code for inserting ,deleting, selecting Data into MySql Db
  • Download Code

Replies are listed 'Best First'.
Re: Need Sample Code for inserting ,deleting, selecting Data into MySql Db
by lamp (Chaplain) on Oct 29, 2008 at 09:16 UTC
Re: Need Sample Code for inserting ,deleting, selecting Data into MySql Db
by moritz (Cardinal) on Oct 29, 2008 at 09:17 UTC
    The DBI module works just the same for updates and inserts, you just supply the data on call to execute:
    my $sth = $dbh->prepare('INSERT INTO table VALUES(?, ?, ?)'); # insert two rows with three numbers each: $sth->execute(3, 4, 5); $sth->execute(6, 7, 8);

    The documentation is quite good, although it contains much more than you need for your first steps. Try to skim over it nonetheless.

    There's also A Short Guide To DBI which might be more suitable for the beginner, and it's quite well written.

    (Update: I first wrote execute instead of prepare, thanks pKai++)