Modifying Data
Commands can be executed individually by the session with a call to "execute()".
Obtaining the Table Object
The query supplied to this method is constructed using the associated Table object of a mapped class. This Table object is obtained from the mapped class's metadata field, as below:
Contacts_table = Contacts.metadata.tables["Contacts"]
Once the table object is obtained, the write operations are executed in the following ways. The queries are executed immediately without the need for a call to "commit()":
Insert
The following example adds a new record to the table:
session.execute(Contacts_table.insert(), {"City": "U.S.A", "Country": "Canada"})
Update
The following example modifies an existing record in the table:
session.execute(Contacts_table.update().where(Contacts_table.c.Id == "123456789").values(City="U.S.A", Country="Canada"))
Delete
The following example removes an existing record from the table:
session.execute(Contacts_table.delete().where(Contacts_table.c.Id == "123456789"))