Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

How to track the success of insert
Hi

I was trying out some practice examples with DBI and CGI and kind of stuck w
hile 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','pos
tgres',{RaiseError=>1,PrintErr
or=>1})
or die "Cannot connect to testdb\n";

my $sth=$dbh->prepare("insert into userinfo values('$fvalue','$lvalue','$ema
il')");
$sth->execute();
if($sth)
{
print $cgi->p( "<script language=Javascript> alert('Execution Done') </scrip
t>" );
}
else
{
print $cgi->p("<Script language=Javascript> alert('Execution Not Done') </sc
ript>" );
}


Report this thread to moderator Post Follow-up to this message
Old Post
Anish Kumar K.
09-29-04 09:58 AM


RE: How to track the success of insert

> -----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.


Report this thread to moderator Post Follow-up to this message
Old Post
NYIMI Jose \
09-29-04 09:58 AM


RE: How to track the success of insert

> -----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>



Report this thread to moderator Post Follow-up to this message
Old Post
NYIMI Jose \
09-29-04 09:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:37 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.