cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Cannot pass a variable into the run method of Neo4j Python Driver

damisg7
Node Clone

In the below code i want to pass variables from the run method of Neo4j Python Driver to the query and then forward these variables to the specific cypher file with the runFile method.

The problem is that the variables url and apiKey are not passed to the query (or not forwarded to the file (?)) when they defined as variables. However when they are passed as strings 'https://services.nvd.nist.gov/rest/json/cpes/2.0' for example the url, everything works pretty good and the nodes i want are created.

Any thoughts??

 

    def getData(self, url, apiKey):
        print("\nInserting CPE Files to Database...")
        # Insert file with CPE Query Script to Database
        query = """call apoc.cypher.runFile("CPE.cypher", {parameters: {APIurl: $APIurl, ParameterName: $ParameterName,
            ParameterValue: $ParameterValue, AuthKey: $AuthKey}})"""
        session = self.driver.session()
        result = session.run(query, APIurl = url, ParameterName = 'startIndex', ParameterValue = '0', AuthKey = apiKey)
        print(result)

 

1 ACCEPTED SOLUTION

damisg7
Node Clone

As always the problem was on the dev side and not on the application - platform side. 

url and apiKey variables that were passed included ' ' at the 0 and last index of the string. A simple cut of these was enough to pass the variables to neo4j and work as it should.

View solution in original post

4 REPLIES 4

Can you share a few lines of your CPE.cypher file?

I looked at the runFile source code. It looks like eventually each statement is executed separately with a 'tx.execute(cypher, params)' statement, with the params as a map containing the parameters passed to the 'runFile' procedure. 

Are you referencing the parameters in your file with a '$' prefix? I was able to get it to work with the following single line cypher file:

return $APIurl as api_url, $ParameterName as param_name, $ParameterValue as param_value, $AuthKey as auth_key;

It returned the values I had passed as parameters.  

Screen Shot 2022-12-27 at 11.35.46 PM.png

Screen Shot 2022-12-27 at 11.37.11 PM.png

These are the first lines of the CPE.cypher.

WITH $APIurl + '?' + $ParameterName + '=' + $ParameterValue AS url,
$AuthKey AS apiKey

call apoc.load.jsonParams(url, { apiKey : apiKey }, null ) yield value

Each statement in your file must terminate with a semicolon. 

damisg7
Node Clone

As always the problem was on the dev side and not on the application - platform side. 

url and apiKey variables that were passed included ' ' at the 0 and last index of the string. A simple cut of these was enough to pass the variables to neo4j and work as it should.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online