-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransaction.py
More file actions
37 lines (28 loc) · 852 Bytes
/
transaction.py
File metadata and controls
37 lines (28 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
from dotenv import load_dotenv
load_dotenv()
from neo4j import GraphDatabase
# Initialize the Neo4j driver
driver = GraphDatabase.driver(
os.getenv('NEO4J_URI'),
auth=(
os.getenv('NEO4J_USERNAME'),
os.getenv('NEO4J_PASSWORD')
)
)
# Verify the connection
driver.verify_connectivity()
# Create a session to run a transaction
with driver.session() as session:
# Create a work unit for the transaction
def create_person(tx, name, age):
result = tx.run("""
CREATE (p:Person {name: $name, age: $age})
RETURN p
""", name=name, age=age)
return result.consume()
# Execute transaction function
summary = session.execute_write(create_person, name='Alice', age=30)
print(summary.counters.nodes_created, 'node(s) created.')
# Close the driver
driver.close()