py2neo.pep249 – Python DB API 2.0 Compatibility

This module provides an implementation of the Python Database API v2.0 (as specified in PEP 249) for Neo4j. The classes here are thin wrappers over the regular py2neo functionality, so will provide a very similar behaviour and experience to the rest of the library.

To get started, create a Connection object using the connect() function:

>>> from py2neo.pep249 import connect
>>> con = connect()

The arguments accepted by this function are identical to those accepted by the Graph class in the core API. Therefore, a URI and any combination of individual settings may be specified.

The Connection object represents a single client-server connection and can maintain a single transaction at any point in time. Transactions are implicitly created through using a method like Cursor.execute() or can be explicitly created using the Connection.begin() method.

Module attributes and functions

py2neo.pep249.apilevel = '2.0'

This module supports version 2.0 of the DB API.

py2neo.pep249.threadsafety = 0

This module is not guaranteed to be thread-safe.

py2neo.pep249.paramstyle = 'cypher'

This module uses the Cypher query language parameter notation. Note that this is not a standard setting according to PEP 249.

py2neo.pep249.connect(profile=None, **settings)[source]

Constructor for creating a connection to the database.

Connection objects

class py2neo.pep249.Connection(profile=None, **settings)[source]

PEP249-compliant connection to a Neo4j server.


True if a transaction is active, False otherwise.


Construct a new Cursor object for this connection.

execute(query, parameters=None)[source]

Execute a query on this connection.

executemany(query, seq_of_parameters)[source]

Execute a query on this connection once for each parameter set.


Begin a transaction.


Commit any pending transaction to the database.


Rollback any pending transaction.


Close the connection immediately.

The connection will be unusable from this point forward; a ProgrammingError exception will be raised if any operation is attempted with the connection. The same applies to all cursor objects trying to use the connection. Note that closing a connection without committing the changes first will cause an implicit rollback to be performed.

Cursor objects

class py2neo.pep249.Cursor(connection)[source]

PEP249-compliant cursor attached to a Neo4j server.

arraysize = 1

Connection to which this cursor is bound.


Field details, each represented as a 7-tuple.


Number of rows affected by the last query executed.


Dictionary of summary information relating to the last query executed.

execute(query, parameters=None)[source]

Execute a query.

executemany(query, seq_of_parameters)[source]

Execute query multiple times with different parameter sets.


Fetch the next record, if available.


record tuple or None


Fetch up to size records.




list of record tuples


Fetch all remaining records.


list of record tuples


Close the cursor immediately.


exception py2neo.pep249.Warning[source]

Exception raised for important warnings like data truncations while inserting, etc.

exception py2neo.pep249.Error[source]

Exception that is the base class of all other error exceptions. You can use this to catch all errors with one single except statement. Warnings are not considered errors and thus should not use this class as base.

exception py2neo.pep249.InterfaceError[source]

Exception raised for errors that are related to the database interface rather than the database itself.

exception py2neo.pep249.DatabaseError[source]

Exception raised for errors that are related to the database.

exception py2neo.pep249.DataError[source]

Exception raised for errors that are due to problems with the processed data like division by zero, numeric value out of range, etc.

exception py2neo.pep249.OperationalError[source]

Exception raised for errors that are related to the database’s operation and not necessarily under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name is not found, a transaction could not be processed, a memory allocation error occurred during processing, etc.

exception py2neo.pep249.IntegrityError[source]

Exception raised when the relational integrity of the database is affected, e.g. a foreign key check fails.

exception py2neo.pep249.InternalError[source]

Exception raised when the database encounters an internal error, e.g. the cursor is not valid anymore, the transaction is out of sync, etc.

exception py2neo.pep249.ProgrammingError[source]

Exception raised for programming errors, e.g. table not found or already exists, syntax error in the SQL statement, wrong number of parameters specified, etc.

exception py2neo.pep249.NotSupportedError[source]

Exception raised in case a method or database API was used which is not supported by the database, e.g. requesting a .rollback() on a connection that does not support transaction or has transactions turned off.