467,201 Members | 1,029 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,201 developers. It's quick & easy.

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

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.
Oct 29 '08 #1
  • viewed: 1637
Share:
3 Replies
nithinpes
Expert 256MB
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
Oct 29 '08 #2
numberwhun
Expert Mod 2GB
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
Oct 29 '08 #3
Icecrack
Expert 100+
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"; 
Oct 30 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by phillip.s.powell@gmail.com | last post: by
2 posts views Thread by bballr@gmail.com | last post: by
11 posts views Thread by refugeedeveloper@yahoo.co.in | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.