简体   繁体   English

Python Neo4j在Cypher语句中使用字符串变量

[英]Python Neo4j using string variables in Cypher statements

I am obviously misunderstanding something about the format of the session.run method in the python Neo4j module. 我显然对python Neo4j模块中session.run方法的格式有误解。

This works: 这有效:

session.run("statement1"
            "statement2"
             "statement3"
             "statement4", variable list)

But now I need to modify the strings externally to the method: eg I would like to do 但是现在我需要在方法的外部修改字符串:例如,我想这样做

s1 = "statement1"
s2 = "statement2"
session.run(s1
            s2
            "statement3"
            "statement4", variable list)

So that I can modify the statement in ways that cannot be handled by the .run method - eg modifying labels on the fly. 这样,我就可以用.run方法无法处理的方式修改语句-例如,即时修改标签。

But I get a syntax error 但是我收到语法错误

I don't understand why this does not work. 我不明白为什么这行不通。 What am I doing wrong? 我究竟做错了什么? How can I introduce variables such as label names into a run command like that? 我如何在这样的运行命令中引入诸如标签名称之类的变量?

Thanks 谢谢


Actual code is this: 实际代码是这样的:

    s1 = "MERGE (a:Animal:Female {tag: {tag}})"
    session.run(s1
             " MERGE (d:Animal:Female {tag: {dam}})"
            "MERGE (s:Animal:Male {tag: {sire}})"
            "MERGE (d)-[:DamTo{dob:{dob}}]->(a)"
            "MERGE (s)-[:SireTo{dob:{dob}}]->(a)" , tag = tag, dob = dob, dam = dam, sire = sire )

Error message: 错误信息:

" MERGE (d:Animal:Female {tag: {dam}})"
                                      ^
SyntaxError: invalid syntax

In Python, adjacent string literals are automatically concatenated. 在Python中,相邻的字符串文字会自动连接在一起。 However, a string variable does not support automatic concatenation. 但是,字符串变量不支持自动串联。

Try changing this: 尝试更改此:

         s1
         " MERGE (d:Animal:Female {tag: {dam}})"

to this: 对此:

         s1 +
         " MERGE (d:Animal:Female {tag: {dam}})"

The other automatic concatenations should still work. 其他自动串联应该仍然有效。

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

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