PeterKaagman has asked for the wisdom of the Perl Monks concerning the following question:
Hi there Monks,
Lately I've been working a lot with storing CSV content (after some parsing of the data) in a database. In a lot of cases the data allready is in the database, in which case I want to update it. Familiar problem I guess. I came up with this solution:
connectDB(); my $insert = "Insert Into table (id,name) Values (?,?)"; my $update = "Update table set name = ? Where id=?"; my $sth_insert = $dbh->prepare($insert) or die $dbh->errstr; my $sth_update = $dbh->prepare($update) or die $dbh->errstr; for my $data (@$data_ref){ if (! $sth_insert->execute( $$data{'id'}, $$data{'name'} ) ){ print 'Insert error: '.$sth_insert->errstr if $debug; print "Doing update\n"; $sth_update->execute( $$data{'name'}, $$data{'id'} ) or die("Insert and Update failed, update: +".$sth_update->errstr ); } } $sth_insert->finish(); $sth_update->finish(); disconnectDB();
As it turns out the $sth_update is never executed, or at least the database does not reflect it... The insert goes as expected. I do get the "Doing update" message. Any ideas on what Im doing wrong here?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: My update on insert error solution
by roboticus (Chancellor) on Dec 30, 2018 at 23:53 UTC | |
|
Re: My update on insert error solution
by poj (Abbot) on Dec 31, 2018 at 14:03 UTC | |
|
Re: My update on insert error solution
by bliako (Abbot) on Dec 31, 2018 at 10:35 UTC | |
|
Re: My update on insert error solution
by erix (Prior) on Dec 31, 2018 at 13:53 UTC | |
|
Re: My update on insert error solution
by PeterKaagman (Beadle) on Dec 31, 2018 at 15:16 UTC | |
|
Re: My update on insert error solution
by PeterKaagman (Beadle) on Dec 31, 2018 at 14:49 UTC |