GridGain Developers Hub

Python Database API Driver

GridGain 9 clients connect to the cluster via a standard socket connection. Clients do not become a part of the cluster topology, never hold any data, and are not used as a destination for compute calculations.

GridGain DB API driver uses the Python Database API.

Getting Started

Prerequisites

To run the Python driver, the following is required:

  • CMake 3.18 or newer to build the driver

  • Python 3.9 or newer (3.9, 3.10, 3.11 and 3.12 are tested)

  • Access to a running Ignite 3 or GridGain 9 node

Limitations

Script execution of SQL statements is not supported in current release.

Installation

To install Python DB API driver, download it from pip.

pip install pygridgain-dbapi

After this, you can import pygridgain_dbapi into your project and use it.

Connecting to Cluster

To connect to the cluster, use the connect() method:

addr =['127.0.0.1:10800']
conn = pygridgain_dbapi.connect(address=addr, timeout=10)

After you are done working with the cluster, remember to always close the connection to it.

conn.close()

Alternatively, you can use the with statement to automatically close the connection when no longer necessary:

with pygridgain_dbapi.connect(address=addr, timeout=10) as conn:
    # work with client

Getting Cursor Object

To work with tables from Pyton client, you use the cursor object that can be retrieved from the connection object:

cursor = conn.cursor()

Similar to the connection, you can use the with statement when getting the cursor:

with conn.cursor() as cursor:
    # work with client

Executing Single Query

The cursor object can be used to execute SQL statements with the execute command:

cursor.execute('CREATE TABLE Person(id int primary key, name varchar, age int)')

Executing a Batched Query

You can use the executemany command to execute SQL queries with a batch of parameters. This kind of operation offers much higher performance than executing individual queries. The example below inserts two rows into the Person table:

# Sample data set
my_data = [[1, "John", 30], [2, "Jane", 32]]

# Insert all data to the Person table
cursor.executemany('INSERT INTO Person VALUES(?, ?, ?)', test_data)

Getting Query Results

The cursor retains a reference to the operation. If the operation returns results (for example, a SELECT), they will also be stored in the cursor. You can then use the fetchone() method to retrieve query results from the cursor:

cursor.execute('SELECT * FROM Person')
data = cursor.fetchone()
print(data)

Working with Transactions

By default, transactions required for database operations are handled implicitly. However, you can disable automatic transaction handling and manually handle commits.

To do this, first, disable autocommit:

conn.autocommit = False

Once autocommit is disabled, you need to commit your operations manually:

cursor.execute('INSERT INTO Person VALUES(3, "Jannet", 33)')
cursor.execute('INSERT INTO Person VALUES(4, "Garnet", 26)')
conn.commit()

Operations that are not committed are sent to the cluster, but not yet written to the table. The table is only updated when the commit method is called. You can roll back all uncommitted operations with the rollback command:

cursor.execute('INSERT INTO Person VALUES(15, "bad_data", 142)')
conn.rollback()