Search code examples
pythonmemgraphdb

How to insert python variables inside query in Memgrpah?


I need to execute the following query:

CREATE (m: Temp {val1: <variable>})

I know there exists save_node functionality but I don't want to use that. I tried the following syntax but it didn't work

connection.execute("CREATE (m: Temp {{val1: variable}}})

This seems to work only if the variable is a for loop iterator.


Solution

  • You are missing a quatation mark at the end of the command as well as qurly brackets around variable. Here is an example:

     from gqlalchemy import Memgraph
    
     print("Connect")
     memgraph = Memgraph()
    
     print("Create a node")
     variable = 100
     memgraph.execute(f"CREATE (m: Temp {{val1: {variable}}});")