pythonneo4jneo4j-python-driver

Passing parameters as dict to tx.run in Neo4j


I'm trying to write a generic function to take a Cypher query and a dictionary of parameters and be able to dynamically run any given query. What I have looks like this:

def _run_cypher(tx, cypher, params = {}):
    results = []
    tx.run(cypher, parameters=params)

With queries that look like this:

'CREATE INDEX ON :$label(filemd5)'

And passing params as

params = {'label': label}

I get this error:

Invalid input '$': expected whitespace or a label name (line 1, column 18 (offset: 17))
"CREATE INDEX ON :$label(filemd5)"

Either I am going wrong, or in this context you can't pass a dict of named parameters to tx.run()...can anyone set me straight? Thanks!


Solution

  • You can't use parameters as label names

    https://neo4j.com/docs/cypher-manual/current/syntax/parameters/

    You will need to create your own generated query with the desired label and safety checks.

    However, you might want to look into the apoc addon, which may allow this.