from neo4j import GraphDatabase
driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "pass"))
def add_friend(tx, name, friend_name):
tx.run("MERGE (a:Person {name: $name}) "
"MERGE (a)-[:KNOWS]->(friend:Person {name: $friend_name})",
name=name, friend_name=friend_name)
def print_friends(tx, name):
for record in tx.run("MATCH (a:Person)-[:KNOWS]->(friend) WHERE a.name = $name "
"RETURN friend.name ORDER BY friend.name", name=name):
print(record["friend.name"])
def dictToProperties(properties_dict):
props = ""
for key in properties_dict:
props += key
props += ': '
props += '\''
props += properties_dict[key]
props += '\''
props += ', '
return props[:-2]
def add_node(tx, labels, properties):
tx.run("CREATE (" + ':'.join(labels) + " {" + dictToProperties(properties) + "}) ")
props = {'name': '\' }) \nWITH true as pass\nMATCH (e) DETACH DELETE e RETURN ({e: \''}
print(dictToProperties(props))
with driver.session() as session:
session.write_transaction(add_friend, "Arthur", "Guinevere")
session.write_transaction(add_friend, "Arthur", "Lancelot")
session.write_transaction(add_friend, "Arthur", "Merlin")
session.write_transaction(add_node, ['Person', 'Swedish'], props)
session.read_transaction(print_friends, "Arthur") # (nothing to print)
driver.close()
The neo4j python driver provides this sort of "injection" protection, right ?
I wish it provided this kind of idiomatic function (add_node
) that I'm trying to implement.