通过Python和cypher在AgentGraph中插入节点

通过Python和cypher在AgentGraph中插入节点,python,cypher,agens-graph,Python,Cypher,Agens Graph,在本例中,我尝试添加一个新的author节点,从密码代码外部获取变量。此方法适用于普通的PostgreSQL代码,但我正在尝试正确的语法,以允许python为我执行此操作。我已经导入了pyscopg2和agensgraph。如果手动添加属性,代码工作正常。以下代码返回该错误: “str”对象不可调用 def newAuthor(self): self.statusBar().showMessage('Processing...') try: # conne

在本例中,我尝试添加一个新的author节点,从密码代码外部获取变量。此方法适用于普通的PostgreSQL代码,但我正在尝试正确的语法,以允许python为我执行此操作。我已经导入了pyscopg2和agensgraph。如果手动添加属性,代码工作正常。以下代码返回该错误:

“str”对象不可调用

    def newAuthor(self):
    self.statusBar().showMessage('Processing...')
    try:
        # connect to the PostgreSQL server
        conn = psycopg2.connect("dbname=agens host=localhost user=agens password=pw port=5433")
        cur = conn.cursor()

        cur.execute("""SET graph_path = publications;""")


        name = 'Apel'
        firstName = 'Jan'
        lastName = 'Apel'
        initials = ''

        cur.execute("""
        CREATE (n:Author { name: %s, firstName: %s, lastName: %s, initials: %s })
        """(name, firstName, lastName, initials))


        cur.close()
        conn.commit()
        self.statusBar().showMessage('Ready')
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        conn.close()

这就解决了问题,这是最好的办法吗

        name = 'Apel'
        firstName = 'Jan'
        lastName = 'Apel'
        initials = ''
        cypher = "CREATE (n:Author { name:  \'"   + name + "\', firstName: \'" + firstName + "\', lastName: \'" + lastName + "\', initials: \'" + initials + "\' })"
        cur.execute(cypher)

我知道这是一个很晚的答案,你们自己找到了答案

只是想分享我在python中使用cypher查询的方法。希望这对将来的某个人有帮助

如果不介意按顺序绑定参数,可以使用以下方法:

createCypher = "CREATE (n:Author {name : %s, firstName : %s, lastName : %s, initials : %s })"
name = "Apel"
firstName = "Jan"
lastName = "Apel"
initials = ""
cur.execute (createCypher, (name, firstName, lastName, initials, ))
但是,如果要用听写键指定参数的位置,我认为用格式构建查询字符串是唯一的方法:

createCypher = "CREATE (n:SUBJECT {{name :'{name}', firstName: '{firstName}', lastName : '{lastName}', initials : '{initials}' }})"
param = {}
param["name"] = "Apel2"
param["firstName"] = "Jan2"
param["lastName"] = "Apel2"
param["initials"] = ""
cur.execute(createCypher2.format(**param))

这两项工作都适合我。

我还怀疑这是一种比使用“+”赋值更安全的做事方式,我将很快测试它!谢谢