in reply to Re^2: Appending text to a column in SQL
in thread Appending text to a column in SQL
Well then you're either left with running a transaction around your read/append/write or CAST the text field to varchar then concat (assuming the data can be cast).
I would go for the transaction approach, you'll be sure it'll work with any kind of data.
As for the ugliness it should be easy to make a method to hide the $dbh->{AutoCommit} = 0 (or $dbh->begin_work), read, append, update, $dbh->commit. Don't forget to turn AutoCommit off if it's on. In fact I would just add that in there to be safe.
UPDATE: something similar to thislocal $dbh->{AutoCommit} = 0; local $dbh->{RaiseError} = 1; # You can RaiseError and eval the transaction # or you can check $sth->err each time and rollback eval { ##Do your read and write here $dbh->commit; }; if ($@) { warn "append failed $@"; $dbh->rollback; }
sub append_to_text { my $dbh = shift; my $table = shift; my $id = shift; my $col = shift; my $data = shift; local $dbh->{RaiseError} = 1; eval { $dbh->begin_work(); my $read = qq| SELECT $col FROM $table WHERE id = ? |; my $update = qq| UPDATE $table SET $col = ? WHERE id = ? |; my $sth_read = $dbh->prepare($read); $sth_read->execute($id); my $row = $sth_read->fetchrow_hashref(); $sth_read->finish(); my $current = $row->{$col}; $current .= $data; my $rtn = $dbh->do($update,undef,$current,$id); $dbh->commit() if ($rtn); }; if ($@) { warn "Append Failed: $@\nStatement: $DBI::lasth->{Statement} +"; eval { $dbh->rollback }; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Appending text to a column in SQL
by sgifford (Prior) on Oct 18, 2006 at 03:06 UTC | |
by grep (Monsignor) on Oct 18, 2006 at 03:59 UTC |