Python
Full API docs can be found on the client SDK project page, but the basics are summarized here.
Using Db2 for IBM i with Python is easy. First, install the package:
next, setup the server credentials used to connect to the server. One way to do this is to create a mapepire.ini
file in the root of your project with the following content:
The following script sets up a DaemonServer
object that will be used to connect with the Server Component. Then a single SQLJob
is created to facilitate the connection from the client side.
Here is the output from the script above:
The results object is a JSON object that contains the metadata and data from the query. Here are the different fields returned:
id
field contains the query IDhas_results
field indicates whether the query returned any resultsupdate_count
field indicates the number of rows updated by the query (-1 if the query did not update any rows)metadata
field contains information about the columns returned by the querydata
field contains the results of the queryis_done
field indicates whether the query has finished executingsuccess
field indicates whether the query was successful.
Configure Connection Details with .ini
file
The connection details can be stored in a .ini
file and passed directly to SQLJob
or PoolJob
objects:
Then pass the path to the .ini
file and the section name to the SQLJob
object:
If section
is not provided, the first section in the .ini
file will be used.
Running Queries
The following examples all assume that the connection details are stored in a .ini
file called mapepire.ini
in the root of the project.
There are four main ways to run queries using mapepire-python
:
- Using the
SQLJob
object to run queries synchronously - Using the
PoolJob
object to run queries asynchronously - Using the
Pool
object to run queries “concurrently” - Using PEP 249 Implementation
1. Using the SQLJob
object to run queries synchronously
Using python context managers, the SQLJob
object can be used to create and run queries synchronously. sql_job
and query
objects are automatically closed after running the query.
Query and run
To create and run a query in a single step, use the query_and_run
method:
2. Using the PoolJob
object to run queries asynchronously
The PoolJob
object can be used to create and run queries asynchronously:
To run a create and run a query asynchronously in a single step, use the query_and_run
method:
3. Using the Pool
object to run queries “concurrently”
The Pool
object can be used to create a pool of PoolJob
objects to run queries concurrently.
This script will create a pool of 3 PoolJob
objects and run the query values (job_name)
concurrently. The results will be printed to the console.
4. Using PEP 249 Implementation
PEP 249 is the Python Database API Specification v2.0. The mapepire-python
client provides a PEP 249 implementation that allows you to use the Connection
and Cursor
objects to interact with the Mapepire server. Like the examples above, we can pass the mapepire.ini
file to the connect
function to create a connection to the server:
fetchmany()
and fetchall()
methods
The Cursor
object provides the fetchmany()
and fetchall()
methods to fetch multiple rows from the result set:
PEP 249 Asynchronous Implementation
The PEP 249 implementation also provides an asynchronous interface for running queries. The connect
function returns an asynchronous context manager that can be used with the async with
statement:
Allow all certificates
On the DaemonServer
interface, the ignoreUnauthorized
set to true
will allow either self-signed certificates or certificates from a CA.