简体   繁体   English

使用 python 在 neo4j 中传递参数

[英]passing parameters in neo4j using python

I want to pass parameter in CREATE using Python eg: '''我想使用 Python 在 CREATE 中传递参数,例如:'''

n = "abc"
a = 1234

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

'''' ''''

but it dosen't work.. any suggestions please但它不起作用..请有任何建议

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).它实际上取决于您用于连接到 Neo4j 的 Python 驱动程序(查看https://neo4j.com/developer/python/以查看可用驱动程序列表)。 If you are using the official neo4j-driver, the code you wrote is correct.如果你使用的是官方的 neo4j-driver,那么你写的代码是正确的。 In order execute the Cypher query, you could do something like this:为了执行 Cypher 查询,您可以执行以下操作:

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.虽然 âńōŋŷXmoůŜ 的答案可能会奏效,但不推荐这样做。

See also :另见

You can use the f-strings in Python.您可以使用 Python 中的 f 字符串。 See example below.请参见下面的示例。 Note that注意

  1. You need to use {{ as escape character for {您需要使用 {{ 作为转义字符 {

2 You need to use \ as escape character for " 2 您需要使用 \ 作为转义字符“

n = "abc"
a = 1234

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

Result:
CREATE (cornell:university {name: "abc", yob: 1234})

reference: https://www.python.org/dev/peps/pep-0498/参考: https://www.python.org/dev/peps/pep-0498/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM