Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing parameters in neo4j using python

Tags:

python

neo4j

I want to pass parameter in CREATE using Python e.g: '''

n = "abc"
a = 1234

cqlCreate = "CREATE (cornell:university { name: $n,yob:$a})"

''''

but it dosen't work.. any suggestions please

like image 291
Sama Avatar asked Sep 02 '26 21:09

Sama


1 Answers

It actually depends on the Python driver that you are using to connect to Neo4j (check https://neo4j.com/developer/python/ to see the list of available drivers). If you are using the official neo4j-driver, the code you wrote is correct. In order execute the Cypher query, you could do something like this:

from neo4j import GraphDatabase

uri = # insert neo4j uri
user = # insert neo4j username
password = # insert neo4j password

n = "abc"
a = 1234
query = "CREATE (cornell:university { name: $n,yob:$a})"

driver = GraphDatabase.driver(uri, auth=(user, password))
session = driver.session()
result = session.run(query, n=n, a=a)
session.close()
driver.close()

Although âńōŋŷXmoůŜ's answer will probably work, it is not recommended way to it.

See also:

  • https://neo4j.com/docs/api/python-driver/current/api.html
like image 170
Jaume Mateu Avatar answered Sep 04 '26 12:09

Jaume Mateu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!