Setting Up Data Buffers

Data buffers are needed to receive query results from the DBMS and to supply arguments to parameterized SQL statements.

This example sets up data buffers to receive the results of a query that returns a single row with two columns:

SELECT name, weight
FROM foo
WHERE ordernr = 71025

Buffers are required for the value of the result columns, and an array of IIAPI_DATAVALUE is needed to point to them.

In this example the allocation of buffers is hard-coded for simplicity. In reality the number, type, and size of the required buffers would generally be determined from the array of IIAPI_DESCRIPTOR returned by IIapi_getDescriptor(), and the buffers would be allocated dynamically.

Note

asyncio is used in this example but it is not essential. To use busy-wait loops instead, see Working with Asynchronous Functions.

Download

 1import asyncio
 2import pyngres.asyncio as py
 3import ctypes
 4...
 5
 6async def exec_select():
 7    ...
 8
 9    # allocate a sufficiently large list of IIAPI_DATAVALUEs  
10    n_columns = 2
11    datavalues = (py.IIAPI_DATAVALUE * n_columns)()
12
13    # allocate buffers for name and weight
14    name = ctypes.c_buffer(25)
15    weight = ctypes.c_double()
16
17    # point to the buffers 
18    datavalues[0].dv_value = ctypes.addressof(name)
19    datavalues[1].dv_value = ctypes.addressof(weight)
20
21    ...

Download