How to track the success of insert
Hi I was trying out some practice examples with DBI and CGI and kind of stuck while doing a comparison That is if I could insert successfully into a databse a script window shld come "Success". but when the insert fails a window shld come saying "Can;t insert ".. How will I track if the insertion is success or not? Below is the program... use strict; use warnings; use CGI; use DBI; my $cgi = new CGI; print $cgi->header( "text/html" ); print $cgi->start_html( "Welcome" ); my $dbh = DBI->connect("DBI:Pg:dbname=testdb;host=localhost",'postgres','postgres',{RaiseError=>1,PrintErr or=>1}) or die "Cannot connect to testdb\n"; my $sth=$dbh->prepare("insert into userinfo values('$fvalue','$lvalue','$email')"); $sth->execute(); if($sth) { print $cgi->p( " alert('Execution Done') " ); } else { print $cgi->p(" alert('Execution Not Done') " ); }
RE: How to track the success of insert
> -Original Message- > From: Anish Kumar K. [mailto:[EMAIL PROTECTED] > Sent: Wednesday, September 29, 2004 9:28 AM > To: beginners perl > Subject: How to track the success of insert > > > Hi > > I was trying out some practice examples with DBI and CGI and > kind of stuck while doing a comparison > > That is if I could insert successfully into a databse a > script window shld come "Success". but when the insert fails > a window shld come saying "Can;t insert ".. > > How will I track if the insertion is success or not? Below is > the program... > > > use strict; > use warnings; > use CGI; > use DBI; > my $cgi = new CGI; > print $cgi->header( "text/html" ); > print $cgi->start_html( "Welcome" ); > my $dbh = > DBI->connect("DBI:Pg:dbname=testdb;host=localhost",'postgres', > 'postgres',{RaiseError=>1,PrintErr > or=>1}) > or die "Cannot connect to testdb\n"; > > my $sth=$dbh->prepare("insert into userinfo > values('$fvalue','$lvalue','$email')"); > $sth->execute(); >if($sth) > { >print $cgi->p( " > alert('Execution Done') " ); > } > else >{ >print $cgi->p(" > alert('Execution Not Done') " ); >} Suggestion: Put your instert inside a transaction like this: [snip] #begin transaction $dbh->begin_work; eval{ #you can decide to combine prepare() and execute() into do() $dbh->do( "insert into userinfo values ('$fvalue','$lvalue','$email')" ); }; #check if the transaction went ok if($@){ $dbh->rollback; print $cgi->p(" alert('Execution Not Done') " ); } else{ $dbh->commit; print $cgi->p( " alert('Execution Done') " ); } Tim Bunce (the Auteur of DBI) gave a nice presentation about "DBI transactions" that you can find from this link: http://search.cpan.org/src/TIMB/DBI_AdvancedTalk_2004/sld054.htm Or from http://dbi.perl.org (Online Documentation) HTH, José. DISCLAIMER "This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer". Thank you for your cooperation. For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
RE: How to track the success of insert
> -Original Message- > From: Anish Kumar K. [mailto:[EMAIL PROTECTED] > Sent: Wednesday, September 29, 2004 9:28 AM > To: beginners perl > Subject: How to track the success of insert > > > Hi > > I was trying out some practice examples with DBI and CGI and > kind of stuck while doing a comparison > > That is if I could insert successfully into a databse a > script window shld come "Success". but when the insert fails > a window shld come saying "Can;t insert ".. > > How will I track if the insertion is success or not? Below is > the program... > > > use strict; > use warnings; > use CGI; > use DBI; > my $cgi = new CGI; > print $cgi->header( "text/html" ); > print $cgi->start_html( "Welcome" ); > my $dbh = > DBI->connect("DBI:Pg:dbname=testdb;host=localhost",'postgres', > 'postgres',{RaiseError=>1,PrintErr > or=>1}) > or die "Cannot connect to testdb\n"; > > my $sth=$dbh->prepare("insert into userinfo > values('$fvalue','$lvalue','$email')"); > $sth->execute(); >if($sth) > { >print $cgi->p( " > alert('Execution Done') " ); > } > else >{ >print $cgi->p(" > alert('Execution Not Done') " ); >} Suggestion: Put your instert inside a transaction like this: [snip] #begin transaction $dbh->begin_work; eval{ #you can decide to combine prepare() and execute() into do() $dbh->do( "insert into userinfo values ('$fvalue','$lvalue','$email')" ); $dbh->commit; }; #check if the transaction went ok if($@){ $dbh->rollback; print $cgi->p(" alert('Execution Not Done') " ); } else{ print $cgi->p( " alert('Execution Done') " ); } Tim Bunce (the Author of DBI) gave a nice presentation about "DBI transactions" that you can find from this link: http://search.cpan.org/src/TIMB/DBI_AdvancedTalk_2004/sld054.htm Or from http://dbi.perl.org (Online Documentation) HTH, José. DISCLAIMER "This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer". Thank you for your cooperation. For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>