Connecting Tech Pros Worldwide Help | Site Map

how to insert a query into MYsql Db residing on my local host

Newbie
 
Join Date: Oct 2008
Posts: 12
#1: Oct 29 '08
Hi Guys, I am a beginner to perl.
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,table:samples with coulms :id,name,phone

I am done with selecting the rows in DB. I need Sample code for inserting , updating and deleting . Please provide some basic infor regarding these.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w 
  2.  
  3. use DBI; 
  4.  
  5. $dbh = DBI->connect('dbi:mysql:perltest','root','sierra') 
  6.  
  7. or die "Connection Error: $DBI::errstr\n"; 
  8.  
  9. $sql = "select * from samples"; 
  10.  
  11. $sth = $dbh->prepare($sql); 
  12.  
  13. $sth->execute 
  14.  
  15. or die "SQL Error: $DBI::errstr\n"; 
  16.  
  17. while (@row = $sth->fetchrow_array) { 
  18.  
  19. print "@row\n"; 
  20.  
  21. }
  22.  
Similarly i Need code for Inserting and updating and deleting operations and all these functions as a package. Can guyz help me in how to proceed for these things.

You can mail me ur proceeding to this id EMAILS REMOVED

Thanks in Advance
Koti.
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#2: Oct 29 '08

re: how to insert a query into MYsql Db residing on my local host


Quote:

Originally Posted by koti688

Hi Guys, I am a beginner to perl.
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,table:samples with coulms :id,name,phone

I am done with selecting the rows in DB. I need Sample code for inserting , updating and deleting . Please provide some basic infor regarding these.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w 
  2.  
  3. use DBI; 
  4.  
  5. $dbh = DBI->connect('dbi:mysql:perltest','root','sierra') 
  6.  
  7. or die "Connection Error: $DBI::errstr\n"; 
  8.  
  9. $sql = "select * from samples"; 
  10.  
  11. $sth = $dbh->prepare($sql); 
  12.  
  13. $sth->execute 
  14.  
  15. or die "SQL Error: $DBI::errstr\n"; 
  16.  
  17. while (@row = $sth->fetchrow_array) { 
  18.  
  19. print "@row\n"; 
  20.  
  21. }
  22.  
Similarly i Need code for Inserting and updating and deleting operations and all these functions as a package. Can guyz help me in how to proceed for these things.

You can mail me ur proceeding to this id EMAILS REMOVED

Thanks in Advance
Koti.

The examples will be available in the documentation for DBD::MySQL in search.cpan.org.
You may go through this link
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,565
#3: Oct 29 '08

re: how to insert a query into MYsql Db residing on my local host


Please DO NOT include any email addresses or contact information in the technical forums. This information is easily scraped by web spiders. This policy exists for your protection. Please read the Posting Guidelines for this site so you are aware of what to and not to do.

Regards,

Moderator
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#4: Oct 30 '08

re: how to insert a query into MYsql Db residing on my local host


well first off this question isn't related to Perl, but to mysql statements

for example look at the var $sql see what has changed from the code you posted.
Expand|Select|Wrap|Line Numbers
  1. use DBI; 
  2.  
  3. $dbh = DBI->connect('dbi:mysql:perltest','root','sierra') 
  4.  
  5. or die "Connection Error: $DBI::errstr\n"; 
  6.  
  7. $sql = "INSERT INTO $tablename (custid, name, repcode, state, contact, phone, warn) VALUES ('$customerid','$customername','$repcode','$state','$contact','$tel','$warnnote') "; 
  8.  
  9. $sth = $dbh->prepare($sql); 
  10.  
  11. $sth->execute 
  12.  
  13. or die "SQL Error: $DBI::errstr\n"; 
Reply