我想使用 Python 在 CREATE 中传递参数,例如:'''
n = "abc"
a = 1234
cqlCreate = "CREATE (cornell:university { name: $n,yob:$a})"
Run Code Online (Sandbox Code Playgroud)
''''
但它不起作用..请提出任何建议
小智 7
它实际上取决于您用于连接 Neo4j 的 Python 驱动程序(检查https://neo4j.com/developer/python/以查看可用驱动程序列表)。如果您使用官方的neo4j驱动程序,则您编写的代码是正确的。为了执行 Cypher 查询,您可以执行以下操作:
\nfrom neo4j import GraphDatabase\n\nuri = # insert neo4j uri\nuser = # insert neo4j username\npassword = # insert neo4j password\n\nn = "abc"\na = 1234\nquery = "CREATE (cornell:university { name: $n,yob:$a})"\n\ndriver = GraphDatabase.driver(uri, auth=(user, password))\nsession = driver.session()\nresult = session.run(query, n=n, a=a)\nsession.close()\ndriver.close()\nRun Code Online (Sandbox Code Playgroud)\n虽然 \xc3\xa2\xc5\x84\xc5\x8d\xc5\x8b\xc5\xb7Xmo\xc5\xaf\xc5\x9c\ 的答案可能有效,但不推荐这样做。
\n也可以看看:
\n\n您可以在 Python 中使用 f 字符串。请参阅下面的示例。注意
- 您需要使用 {{ 作为 { 的转义字符
2 您需要使用 \ 作为“的转义字符”
n = "abc"
a = 1234
cqlCreate = f"CREATE (cornell:university {{name: \"{n}\", yob: {a}}})"
print (cqlCreate)
Result:
CREATE (cornell:university {name: "abc", yob: 1234})
Run Code Online (Sandbox Code Playgroud)
参考: https: //www.python.org/dev/peps/pep-0498/
| 归档时间: |
|
| 查看次数: |
2540 次 |
| 最近记录: |