By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

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

P: 11
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
Share this Question
Share on Google+
3 Replies


nithinpes
Expert 100+
P: 410
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 2.5K+
P: 3,503
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+
P: 174
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.