在此示例中,我尝试添加一个新的作者节点,该节点从密码代码外部获取变量。这种方法适用于普通的 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()