使用Python将列表作为neo4j中的参数传递

kxeu7u2r  于 2022-11-05  发布在  Python
关注(0)|答案(1)|浏览(152)

我正在尝试运行参数为列表的查询:

codes = ["123","234"]
query = graph.run("""
     MATCH (n:NAME)
     where n.codes = "{}"
     RETURN return n.commonName as commonName""".format(codes))

我尝试将“codes”作为参数传递给Neo4j查询。这会引发“list is not readable in the query”(列表在查询中不可读)的错误
如有任何建议,我们将不胜感激。

yeotifhr

yeotifhr1#

您可以使用下面的代码,我正在使用我的测试:

from py2neo import Graph 
session = Graph("http://localhost:7474", auth=("neo4j", "*****"))
//create a dictionary of key:value
param = {"codes":["Angy","Rodney"]}
//use $<dict_key> in your query. For ex: $codes
query = """
     MATCH (n:Customer)
     where n.Name in $codes
     RETURN n"""
//Run the result
result = session.run(query, parameters=param).data() 
//Below are my debugging/print to check what is inside the node

print ("What is data type of result? ", type(result))
print ("What is the data type of each item? ", type(result[0]))
print ("What are the keys of the dictionary? ", result[0].keys())
print ("What is the class of the node? ", type(result[0].get('n')))
print ("How to access the first node? ", result[0].get('n'))
print ("How to access values inside the node? ", result[0].get('n',{}).get('Name'))

Result:
What is data type of result?  <class 'list'>
What is the data type of each item?  <class 'dict'>
What are the keys of the dictionary?  dict_keys(['n'])
What is the class of the node?  <class 'py2neo.data.Node'>
How to access the first node?  (_327:Customer {Latitude: 48.509075, Longitude: -2.7383235, Name: 'Angy'})
How to access values inside the node?  Angy

相关问题