perl - DBD::SQLite: Insert or Update - Question -
while writing question (howto: insert new entry , if exists already, update it) found answers in related questions
:
$sql = "insert or replace $table ( id, name, rating ) values( ?, ?, ? )"; $sth_rating = $dbh->prepare( $sql ); $sth_rating->execute( $id, $name, $rating );
.
$sql = "insert or ignore $table ( id, name, rating ) values ( ?, ?, ? )"; $sth_rating = $dbh->prepare( $sql ); $sth_rating->execute( $id, $name, $rating ); $sql = "update $table set rating = ? id = ?"; $sth_rating = $dbh->prepare( $sql ); $sth_rating->execute( $rating, $id );
is second method more safe first one?
the second method less safe because non-atomic. in other words, happens in more 1 step. consider 2 processes both updating same data. time moving down.
process 1 process 2 insert or ignore... insert or ignore... update... update...
process 1 starts first, process 2 sneaks in , update between. process 1 blows right on process 2's update.
this isn't bad in particular situation, both processes going blow on each other no matter what, can trouble extending technique.
(unless misunderstood question , want upsert)
Comments
Post a Comment