Modifying Data
The connection is also used to issue INSERT, UPDATE, and DELETE commands to the data source. Parameters can be used with these statements if desired.
Note that the connector does not support transactions. As with normal write operations, all SQL statements executed by this connector affect the data source immediately. Call the connection's commit() method following the execution.
Insert
The following example adds a new record to the table:cmd = "INSERT INTO \"postgres\".\"schema01\".Orders (ShipName, ShipCity) VALUES (?, ?)" params = ["Raleigh", "New York"] cur = conn.execute(cmd, params) print("Records affected: ", cur.rowcount)
Update
The following example modifies an existing record in the table:cmd = "UPDATE \"postgres\".\"schema01\".Orders SET ShipCity = ? WHERE Id = ?" params = ["New York", "10261"] cur = conn.execute(cmd, params) print("Records affected: ", cur.rowcount)
Delete
The following example removes an existing record from the table:
cmd = "DELETE FROM \"postgres\".\"schema01\".Orders WHERE Id = ?" params = ["10261"] cur = conn.execute(cmd, params) print("Records affected: ", cur.rowcount)