Coding using C through hiredis

I want to use hiredis to code with redisgraph in C. However, according to the schema given in the redisgraph project on GitHub, this is my way:
redisReply *reply = redisCommand(graph, “GRAPH.QUERY MotoTest “CREATE (:Rider {name:‘Valentino Rossi’})-[:rides]->(:Team {name:‘Yamaha’}), (:Rider {name:‘Dani Pedrosa’})-[:rides]->(:Team {name:‘Honda’}), (:Rider {name:‘Andrea Dovizioso’})-[:rides]->(:Team {name:‘Ducati’})””);
The ’ " ’ causing a list of problem, and I can’t solve it by myself. This is the error message:
ERR wrong number of arguments for ‘GRAPH.QUERY’ command
How could I change the query statement to deliver correct information to redis-server?

Sorry, I missed two backslash, as it’s necessary to insert a " between a C-style string. This is my way:
redisReply *reply = redisCommand(graph, “GRAPH.QUERY MotoTest \“CREATE
(:Rider {name:‘Valentino Rossi’})-[:rides]->(:Team {name:‘Yamaha’}), (:Rider
{name:‘Dani Pedrosa’})-[:rides]->(:Team {name:‘Honda’}), (:Rider {name:‘Andrea
Dovizioso’})-[:rides]->(:Team {name:‘Ducati’})\””);

My English is poor, so I am afraid that you can’t understand me. What I am eager to know is that, in this sentence: GRAPH.QUERY KEYNAME “CREATE…” why is CREATE… wrapped in double quotation marks, did that mean that content between them is a character string? How could I modify my way using hiredis’ API would help me deliver correct message to GRAPH.QUERY? Thank you for your time and consideration.

The GRAPH.QUERY command in RedisGraph takes two arguments. The first is the key containing the graph. The second is the query against that graph. If you were to use redis-cli to execute your query it would look like this:

> GRAPH.QUERY MotoTest “CREATE
    (:Rider {name:‘Valentino Rossi’})-[:rides]->(:Team {name:‘Yamaha’}),
    (:Rider {name:‘Dani Pedrosa’})-[:rides]->(:Team {name:‘Honda’}),
    (:Rider {name:‘Andrea Dovizioso’})-[:rides]->(:Team {name:‘Ducati’})”

The hiredis redisCommand function is variadic but takes a minimum of two arguments. The first is the context, or the connection to Redis. The next is the string to be passed to Redis that is the command being executed. In this case, the command itself is made up of several arguments. And, the argument to Redis that contains the query has spaces in it, so it needs to be surrounded in quotes. Therfore, you need those in your code too.

The extra arguments on redisCommand are used for string interpolation and can help to make your code more readable (among other things). This is the %s stuff we’re familiar with from printf.

I would take advantage of string interpolation and call ‘redisCommand’ like this instead:

char *key = "MotoTest";
char *query =
    "CREATE"
        "(:Rider {name:‘Valentino Rossi’})-[:rides]->(:Team {name:‘Yamaha’}),
        "(:Rider {name:‘Dani Pedrosa’})-[:rides]->(:Team {name:‘Honda’}),
        "(:Rider {name:‘Andrea Dovizioso’})-[:rides]->(:Team {name:‘Ducati’})";

redisReply *reply = redisCommand(context, "GRAPH.QUERY %s \"%s\"", key, query);

This is probably easier to read and understand for us humans. :wink:

1 Like

At first, thank you for your help! But I already tried the %s stuff, unfortunately it didn’t work. Here is the errMessage when I tried the printf way;

errMsg: Invalid input ‘"’: expected ‘;’, a statement option, a query hint, a clause or a schema command line: 1, column: 1, offset: 0 errCtx: "CREATE(:Rider {name:‘Valentino Rossi’})-[:rides]->(:Team {name:‘Yamaha… errCtxOffset: 0

I am desparing, maybe delivering a C-style string through redisCommand to redisgraph is not useful :sob:

Do you have a repository with this code that I could look at?