Home > Archive > PERL Beginners > September 2004 > How to track the success of insert
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to track the success of insert
|
|
| Anish Kumar K. 2004-09-29, 4:58 am |
| 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( "<script language=Javascript> alert('Execution Done') </script>" );
}
else
{
print $cgi->p("<Script language=Javascript> alert('Execution Not Done') </script>" );
}
| |
| NYIMI Jose \ 2004-09-29, 4:58 am |
|
> -----Original Message-----
> From: Anish Kumar K. [mailto:anish@vitalect-india.com]=20
> Sent: Wednesday, September 29, 2004 9:28 AM
> To: beginners perl
> Subject: How to track the success of insert
>=20
>=20
> Hi
>=20
> I was trying out some practice examples with DBI and CGI and=20
> kind of stuck while doing a comparison
>=20
> That is if I could insert successfully into a databse a=20
> script window shld come "Success". but when the insert fails=20
> a window shld come saying "Can;t insert "..
>=20
> How will I track if the insertion is success or not? Below is=20
> the program...
>=20
>=20
> use strict;
> use warnings;
> use CGI;
> use DBI;
> my $cgi =3D new CGI;
> print $cgi->header( "text/html" );
> print $cgi->start_html( "Welcome" );
> my $dbh =3D=20
> DBI->connect("DBI:Pg:dbname=3Dtestdb;host=3Dlocalhost",'postgres',
> 'postgres',{RaiseError=3D>1,PrintErr
> or=3D>1})
> or die "Cannot connect to testdb\n";
>=20
> my $sth=3D$dbh->prepare("insert into userinfo=20
> values('$fvalue','$lvalue','$email')");
> $sth->execute();
> if($sth)
> {
> print $cgi->p( "<script language=3DJavascript>=20
> alert('Execution Done') </script>" );
> }
> else
> {
> print $cgi->p("<Script language=3DJavascript>=20
> alert('Execution Not Done') </script>" );
> }
Suggestion:
Put your instert inside a transaction like this:
[snip]
#begin transaction
$dbh->begin_work;
eval{ =09
#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($@){=09
$dbh->rollback;
print $cgi->p("<Script language=3DJavascript> alert('Execution Not Done') =
</script>" );
}
else{
$dbh->commit;
print $cgi->p( "<script language=3DJavascript> alert('Execution Done') </s=
cript>" );=09
}
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...2004/sld054.htm
Or from
http://dbi.perl.org (Online Documentation)
HTH,
Jos=E9.
**** DISCLAIMER ****
"This e-mail and any attachment thereto may contain information which is co=
nfidential and/or protected by intellectual property rights and are intende=
d for the sole use of the recipient(s) named above.=20
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.=20
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.
| |
| NYIMI Jose \ 2004-09-29, 4:58 am |
|
> -----Original Message-----
> From: Anish Kumar K. [mailto:anish@vitalect-india.com]
> Sent: Wednesday, September 29, 2004 9:28 AM
> To: beginners perl
> Subject: How to track the success of insert
>=20
>=20
> Hi
>=20
> I was trying out some practice examples with DBI and CGI and
> kind of stuck while doing a comparison
>=20
> That is if I could insert successfully into a databse a
> script window shld come "Success". but when the insert fails=20
> a window shld come saying "Can;t insert "..
>=20
> How will I track if the insertion is success or not? Below is
> the program...
>=20
>=20
> use strict;
> use warnings;
> use CGI;
> use DBI;
> my $cgi =3D new CGI;
> print $cgi->header( "text/html" );
> print $cgi->start_html( "Welcome" );
> my $dbh =3D
> DBI->connect("DBI:Pg:dbname=3Dtestdb;host=3Dlocalhost",'postgres',
> 'postgres',{RaiseError=3D>1,PrintErr
> or=3D>1})
> or die "Cannot connect to testdb\n";
>=20
> my $sth=3D$dbh->prepare("insert into userinfo
> values('$fvalue','$lvalue','$email')");
> $sth->execute();
> if($sth)
> {
> print $cgi->p( "<script language=3DJavascript>=20
> alert('Execution Done') </script>" );
> }
> else
> {
> print $cgi->p("<Script language=3DJavascript>=20
> alert('Execution Not Done') </script>" );
> }
Suggestion:
Put your instert inside a transaction like this:
[snip]
#begin transaction
$dbh->begin_work;
eval{ =09
#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($@){=09
$dbh->rollback;
print $cgi->p("<Script language=3DJavascript> alert('Execution Not =
Done') </script>" );
}
else{
print $cgi->p( "<script language=3DJavascript> alert('Execution Done') =
</script>" );=09
}
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...2004/sld054.htm
Or from
http://dbi.perl.org (Online Documentation)
HTH,
Jos=E9.
**** 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.=20
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.=20
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.
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org =
<http://learn.perl.org/> <http://learn.perl.org/first-response>
|
|
|
|
|