in reply to Superior way to update SQL tables
Second, what database are you using? MySQL has the REPLACE command, which was designed to do what you're asking. Otherwise, you can depend on your primary and unique keys to fail appropriately, allowing you to do something like:
my $dbh = DBI->connect( $dsn, $user, $password, { PrintError => 0, RaiseError => 1, }, ) or die $DBI::errstr; foreach (@whatever) { eval { $dbh->do( $insert_statement, {}, @values ); }; if ($@) { $dbh->do( $update_statement, {}, @values ); } }
The theory is that if you insert something that isn't there yet, you're fine. If it's there, the primary and unique keys will complain, so you do an update.
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Superior way to update SQL tables
by radiantmatrix (Parson) on Sep 08, 2004 at 15:28 UTC | |
by dragonchild (Archbishop) on Sep 08, 2004 at 15:49 UTC |