Search code examples
pythonneo4jpy2neo

py2neo to BOLT: how to convert tx.append statements to BOLT


In Py2neo there is the ability to append transactions and then commit them as one block to the server

from py2neo import Graph
graph = Graph()
tx = graph.cypher.begin()
stmt1 = "CREATE (:Person {name: 'Guinevere'})"
stmt2 = "CREATE (:Person {name: 'Tom'})"
stmt3 = "CREATE (:Person {name: 'Anna'})"
tx.append(stmt)
tx.append(stmt2)
tx.append(stmt3)
tx.commit()

I can't seem to find the equivalent syntax in the BOLT Neo4j-driver manual to get it to work. Append is not recognized.

driver = GraphDatabase.driver("bolt://localhost",
                                     auth=basic_auth('neo4j', 'password'),
                                     encrypted=True,
                                     trust=TRUST_ON_FIRST_USE)
session = driver.session()    
tx = session.begin_transaction()
tx.append(stmt1) --this does not work
tx.append(stmt2) --this does not work
tx.append(stmt3) --this does not work
tx.commit() --this does not work

What is the right way to do this?


Solution

  • well I've tryed as you seeing below and it worked:

    from neo4j.v1 import GraphDatabase

    driver = GraphDatabase.driver("bolt://localhost")

    session = driver.session()

    stmt1 = "CREATE (:Person {name: 'Guinevere'})"

    stmt2 = "CREATE (:Person {name: 'Tom'})"

    stmt3 = "CREATE (:Person {name: 'Anna'})"

    tx = session.begin_transaction()

    tx.run(stmt1) # returned <neo4j.v1.session.StatementResult object at 0x7f3838f77a58>

    tx.run(stmt2) # returned <neo4j.v1.session.StatementResult object at 0x7f3838f77a58>

    tx.run(stmt3) # returned <neo4j.v1.session.StatementResult object at 0x7f3838f77e10>

    tx.commit() # worked ;)

    ref: https://neo4j.com/docs/api/python-driver/current/#example