From Petl
The connector can be used to create ETL applications and pipelines for CSV data in Python using Petl.
Install Required Modules
Install the Petl modules using the pip utility.pip install petl
Connecting
After you import the modules, including the CData Python Connector for DB2, you can use the connector's connect function to create a connection using a valid DB2 connection string. If you prefer not to use a direct connection, you can use a SQLAlchemy engine.import petl as etl import cdata.db2 as mod cnxn = mod.connect("Server=10.0.1.2;Port=50000;User=admin;Password=admin;Database=test")
Extract, Transform, and Load the DB2 Data
Create a SQL query string and store the query results in a DataFrame.sql = "SELECT Id, Author FROM \"Sample\".\"DB2INST1\".Books " table1 = etl.fromdb(cnxn,sql)
Loading Data
With the query results stored in a DataFrame, you can load your data into any supported Petl destination. The following example loads the data into a CSV file.etl.tocsv(table1,'output.csv')
Modifying Data
Insert new rows into DB2 tables using Petl's appenddb function.table1 = [['Id','Author'],['Jon Doe','John']] etl.appenddb(table1,cnxn,'\"Sample\".\"DB2INST1\".Books')