473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Perl DBI and Inserting

savanm
85
Hi all,

Here I want to insert a row into a table.

Am using Mysql.How to use insert query in PERLDBI

Here is my sample Code

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use DBI;
  3.  
  4. my $dbh = DBI->connect("DBI:mysql:database=perldbi;host=localhost","root","");
  5.  
  6. #$dbh->do("CREATE TABLE asdf (name VARCHAR(21),phone VARCHAR(21))");
  7. #$dbh->do("DROP TABLE asdf");
  8.  
  9. my ($userid, $pass);
  10. my $sth = $dbh->prepare("SELECT * FROM foo1");
  11. $sth->execute();
  12. $sth->bind_columns(\$userid, \$pass);
  13. while ($sth->fetch) {
  14.     print "$userid        $pass\n";
  15. }
  16. $sth->finish();
  17.  
  18. my $sthC = $dbh->prepare("insert TABLE (usrid,pass) values (navi,sava)");
  19. # or die "Couldn't prepare: $DBI::errstr";
  20. $sthC->execute('navi', 'sava');
  21. # or die "Couldn't execute: $DBI::errstr";
  22. $dbh->disconnect();
  23.  
  24.  
Thanks
Mar 30 '07 #1
4 1464
KevinADC
4,059 Expert 2GB
It should be covered in the Mysql driver documentation:

cpan DBD::Mysql
Mar 30 '07 #2
savanm
85
Hi Kevin,

I got the solution,

Expand|Select|Wrap|Line Numbers
  1. print "Enter user id\n";
  2.  my $usr = <STDIN>;    
  3.  chomp($usr);
  4.  print "Enter Password\n";
  5.  my $pas = <STDIN>;
  6.  chomp($pas);
  7.  
  8. my $d=$dbh->do("INSERT INTO foo1 values('$usr','$pas')");
  9. print "Insert $d Rows\n";
  10.  
Thaks for ur help
Mar 30 '07 #3
KevinADC
4,059 Expert 2GB
Very good, you're welcome.
Mar 30 '07 #4
miller
1,089 Expert 1GB
Hi savanm,

A better way to do that insert statement would be to use placeholders like this:

Expand|Select|Wrap|Line Numbers
  1. my $sthC = $dbh->prepare("INSERT INTO foo1 (usrid, pass) VALUES (?, ?)");
  2. my $count = $sthC->execute('navi', 'sava') or die $dbh->errstr;
  3.  
You were close to this method in your original solution. You simply hadn't put in the question marks.

- Miller
Mar 30 '07 #5

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

Similar topics

2
by: R. Gregg Reed | last post by:
I'm trying to intergrate a cgi affiliate tracking system into a php website. When someone pays using PayPal it's supposed to direct them to one page if the payment is a success or another if...
4
by: banz | last post by:
Hello I have a problem to resolve: I wrote a Perlscript which caches data from a server (local on my machine) I would like to have a other connection to a remote server but I don't know how to...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
6
by: www.gerardvignes.com | last post by:
Hi, Have you run into a situation where you had to switch from PHP 4.x.x to Perl 5.x.x in order to get better performance? I am using an OO approach to PHP for my website's server code. There...
0
by: richat | last post by:
hi everyone ,i hav this problm that this code doesn't insert data into the database. It says that active perl not installed or some error in the code .plz help me out .Its urgent. ...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
22
by: Dan Rumney | last post by:
Hi all, I've been writing Javascript for quite a while now and have, of late, been writing quite a lot of AJAX and AJAX-related code. In the main, my dynamically generated pages are created...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
82
by: happyse27 | last post by:
Hi All, I modified the user registration script, but not sure how to make it check for each variable in terms of preventing junk registration and invalid characters? Two codes below : a)...
0
by: Michael C | last post by:
"John Brock" <jbrock@panix.comwrote in message news:gg56ab$qsn$1@reader1.panix.com... You really need to stop trying to write perl code in dot net and VBA. Arrays are hard to resize for a good...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.