On May 19, 2011, at 6:10 PM, jeff wrote:

> I have an app that does an update or insert (can't guarantee the entry
> exists to start with).  I'm trying to increase efficiency and notice a
> commit after every insert or update.  Is there a way to hold off on
> the commit until one final commit?  Not using the ORM for this
> particular piece.

The Engine and Connection offer autocommit behavior by default for INSERT, 
UPDATE, DELETE statements as well as DDL.  Background on how to control this is 
at:

http://www.sqlalchemy.org/docs/core/connections.html#using-transactions




> 
> create a dict with the updated values:
> 
> ...
>         slotargs['part_num']     = row[DB_PART_NUM]
>         slotargs['family']       = row[DB_FAMILY]
> ...
> create the update object (where slot is the Table object):
> 
>         u = slot.update().where( and_(slot.c.host_name ==
> self.HostName, slot.c.row_num == row[DB_ROW_NUM],
> slot.c.col_num==row[DB_COLUMN_NUM],
>                                       slot.c.tray_num ==
> row[DB_TRAY_INDEX],slot.c.port_num ==
> row[DB_PORT_INDEX] ) ).values(**slotargs)
> 
>         self.GCDB.updateOrInsert(u, slot, slotargs)
> 
> .... function body for updateOrInsert....
> 
>   # Arguments are:
>   #  SQL Alchemy update object bound to a table and database
> connection.
>   #  SQL ALchemy table object associated with above update
>    #  Dictionary of columns that are being updated, should include
> the primary key column(s) in case we do the insert
>   #
>   # Returns the result object.
> 
> #--------------------------------------------------------------------------
>   def updateOrInsert(self, updObj, tableObj, columns):
>      result = updObj.execute()                    # Attempt an update
>      if result.rowcount == 0:                     # See if it
> succeeded
>         i = tableObj.insert().values(**columns)   # If not then
> create an insert object, columns must have primary keys for this table
>         result = i.execute()                      # Now do the insert
>      return result
> 
> 
> Thanks for the help!
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to