CData Python Connector for DB2

Build 20.0.7745

Updating Data

The connection's cursor object is used to issue INSERT, UPDATE, and DELETE commands to the data source. If desired, parameters are used with these statements. Unlike many python connectors, this connector does not support transactions. Consequently, any SQL statements executed by this connector affect the data source immediately. So, the commit() method on the connection object should not be used.

Insert

The following example adds a new record to the table:

import cdata.DB2 as mod
conn = mod.connect("Server=10.0.1.2;Port=50000;User=admin;Password=admin;Database=test")
cur = conn.cursor()
cmd = "INSERT INTO Books (Id, Author) VALUES (?, ?)"
params = ["Jon Doe", "John"]
cur.execute(cmd, params)
print("Records affected: ", cur.rowcount)

Update

The following example modifies an existing record in the table:

import cdata.DB2 as mod
conn = mod.connect("Server=10.0.1.2;Port=50000;User=admin;Password=admin;Database=test")
cur = conn.cursor()
cmd = "UPDATE Books SET Author = ? WHERE Id = ?"
params = ["John", "22"]
cur.execute(cmd, params)
print("Records affected: ", cur.rowcount)

Delete

The following example removes an existing record from the table:

import cdata.DB2 as mod
conn = mod.connect("Server=10.0.1.2;Port=50000;User=admin;Password=admin;Database=test")
cur = conn.cursor()
cmd = "DELETE FROM Books WHERE Id = ?"
params = ["22"]
cur.execute(cmd, params)
print("Records affected: ", cur.rowcount)

Copyright (c) 2021 CData Software, Inc. - All rights reserved.
Build 20.0.7745