Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert execution check

Status
Not open for further replies.

venkatpavan

Programmer
Feb 18, 2006
42
SG
Hi,

I'm trying to check whether my insert statement executes successfully or not,Depending on this I'm using the counter,Below is the insert statement from my script,


$sql_Insert = qq {INSERT INTO TABLE VALUES(A,B,C,D)};

$sth_Insert = $dbconnection -> prepare ($sql_Insert) || die print "Couldn't prepare SQL query : $! :$line\n";
$sth_Insert -> execute || print "Couldn't execute SQL query: $!:$line\n";
if ($insert_results > 0){
print "Insert Successfull : $line\n";
$insertSuccessRecords++;
}
else{
print " Insert Failed : $line\n";
$insertFailRecords++;
}

Above condition always going through Insert successful even though insert fails,Please advice.

Thanks,
 
maybe you should be doing
$insert_results = $sth_Insert -> execute || print "Couldn't execute SQL query: $!:$line\n"; if ($insert_results > 0){

Start using
use strict;

It will save you many problems.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
How about checking the return code in $DBI::err with the corresponding error message in $DBI::errstr? Also the number of rows inserted/updated/deleted/selected(fetched)is in $DBI::rows.
 
Thank you both,

travs69 - I'm using use strict in the script,This is the just Insert part in the script which I mentioned above.

dkyrtata - Sorry ,I Just started working on PERL,So how could I check return code in the script?

Thanks,
 
Just check the variables set by DBI

Code:
if ($DBI::err != 0) {   
    print "$DBI::errstr\n"; # Show database error message
}
else {
   print "$DBI::rows rows INSERTed/UPDATEd/DELETEd/SELECTed\n"; # show number of rows affected
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top