Igor Tandetnik wrote:
Eric S. Johansson <[EMAIL PROTECTED]> wrote:
I'm trying to figure out how to do do the following:

if the record exists
  Update record with calculation ( a = a + v)
else
  insert record with default values

It seems like the "insert or replace into" capabilities is close to
what I need but I can't figure out how to update with a default on
the first record and update with a calculation on existing records.

I can't think of any way to avoid two requests. Run the update statement, use sqlite3_changes to check whether any row has actually been updated. If not, run the insert.

since I'm using pysqlite, I'm using the following model (which will probably make sql knowledgeable folks cringe)

-----------
insert_command = 'insert into test (x, y, z) values (?,?,?)'
update_command='update test set z = z + ? where (x=? and y=?)'
try:
   self.cursor.execute(insert_command, (ext_map, ID, 0.0))
except Exception, error:
   self.cursor.execute(update_command,(4.0, ext_map, ID,))
self.connection.commit()

-----------

if the insert fails (i.e. record exists), it triggers an exception which I use to trigger an update. I get many more updates than inserts of course but I haven't figured out how to trigger an exception on update if the record doesn't exist.

--
Speech-recognition in use.  It makes mistakes, I correct some.

-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------

Reply via email to