What is the best way to do batch insert/create/merge by using py2neo?

Since I have read a very nice topic from Michael Hunger, I was trying to make it works with py2neo.

Then I tried to set the parametes by sending a Cypher statement as followed:

cypher = ':param batch:[{name:"Alice",age:32},{name:"Bob",age:42}]'

This will cause a error as followed:

py2neo.database.ClientError: SyntaxError: Invalid input ':': expected <init> (line 1, column 1 (offset: 0))

But if just pasted the cypher onto the neo4j browser, it will just set the parameters successfully.
Can anyone tell me what is wrong with that and is there any other ways to make the insertion more efficient and fast in python?

By the way, the neo4j server reads data from mysql dynamically in real time, so that I can keep both databases' data synchronous.


this should work:
':param batch=> [{name:"Alice",age:32},{name:"Bob",age:42}]'

Thanks, but I tried this way and it not works..

I asked this a while back: How does Py2Neo use parameters to make repeat queries faster?


cypher = 'merge (u:User {name:{name1},age:{age1}}) merge (u2:User {name:{name2},age:{age2}})'
graph.run(cypher, {"name1":"Alice","name2":"Bob","age1":32,"age2":42})

Hi, this did work, here is my code:

batch = [{name:"Alice",age:32},{name:"Bob",age:42}]
graph.run(query, rel_batch)


1 Like

Your original syntax uses a client command, which exists only in applications such as the browser and the Cypher shell; these commands are not part of Cypher itself. The final code you posted it indeed the correct way to do this: passing the parameters along with the Cypher query string as a second argument.

I see the difference between browser and Cypher itself. Thanks sooo much!

batch = [{name:"Alice",age:32},{name:"Bob",age:42}]
graph.run(query, rel_batch)

Hello, can you explain, what exactly does query and rel_batch mean? Can you give an example?

Hi, Im sorry there is a mistake, it shoule be:

batch = [{name:"Alice",age:32},{name:"Bob",age:42}]

query = """
UNWIND {{batch}} as row
your query-

graph.run(query, batch=batch)

1 Like