【问题标题】:Can elements of a list be used for a graph node's properties?列表的元素可以用于图形节点的属性吗?
【发布时间】:2018-05-11 11:07:18
【问题描述】:

我对使用 Neo4j 和 Python 还是很陌生。我想知道是否可以在 Python 和 Neo4j 中使用列表元素作为图形节点的属性。

我尝试创建一个配方节点,但在“createNode”变量中出现错误。我究竟做错了什么?

这是我的代码:

# a list containing a list of recipes and their respective information 
recipeList = [['Mac and Cheese', 'Anytime', 'Macaroni', 'Terry'], ['Chicken Curry', 'Supper', 'Chicken', 'Anne']]

def print_recipes(self,aList):
    with self._driver.session() as session:
       recipes = session.write_transaction(self.createRecipeNodes,aList)
            print(recipes)


def createRecipeNodes(tx, aRecipeList):

    for allRecipes in aRecipeList:
        #for recipe in allRecipes:
        recipeName = allRecipes[0]
        serveTime = allRecipes[1]
        mainIngrediant = allRecipes[2]
        givenBy =  allRecipes[3]

        createNode = tx.run("CREATE (theNode:Recipe {recipeName = {recipeName}, serveTime = {serveTime}, mainIngrediant = {mainIngrediant}, givenBy = {givenBy}" ,recipeName=recipeName,serveTime=serveTime,mainIngrediant=mainIngrediant,givenBy=givenBy)

我得到的错误是这样的:

neo4j.exceptions.CypherSyntaxError: Invalid input '=': expected whitespace, comment, ':' or '}' (line 1, column 36 (offset: 35)) "CREATE (theNode:Recipe {recipeName = {recipeName}, serveTime = {serveTime}, mainIngrediant = {mainIngrediant}, givenBy = {givenBy}

提前谢谢你!

【问题讨论】:

  • 错误是什么?这比让我们猜测要容易。
  • 抱歉,我得到的错误是:neo4j.exceptions.CypherSyntaxError: Invalid input '=': expected whitespace, comment, ':' or '}' (第 1 行,第 36 列(偏移量: 35)) "创建 (theNode:Recipe {recipeName = {recipeName}, serveTime = {serveTime}, mainIngrediant = {mainIngrediant}, givenBy = {givenBy}"
  • 把它放在问题中,格式正确。
  • 请注意,更好的方法是创建一个映射列表,每个映射包含 recipeName、serveTime、mainIngredient 和 givenBy 的键。这样,您可以使用地图列表作为参数进行一次调用,而不是每次创建都使用循环,然后在查询中 UNWIND 列表并执行单个 CREATE (它将按行执行,因此它将适用于列表中的所有元素)。详情请见these tips and tricks

标签: python neo4j


【解决方案1】:

Cypher 查询不使用= 进行属性分配,而是使用:

createNode = tx.run("CREATE (theNode:Recipe {recipeName: {recipeName}, serveTime: {serveTime}, mainIngrediant: {mainIngrediant}, givenBy: {givenBy}"
 ,recipeName=recipeName,serveTime=serveTime,mainIngrediant=mainIngrediant,givenBy=givenBy)

【讨论】:

    最近更新 更多