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.

How to handle singleQuotes in Variable for Cypher Statement

CodeCase
Node Link

Hi together,

I try to save a value with a Cypher Query and Nei4j v4 and Neo4j.Driver in C#

My problem is that the value of my variable contains a singlequote, for example like:

ÖöÜüÄäßµ@€-&'.Sb34

Thats my Query:

var Name = "ÖöÜüÄäßµ@€-&'.Sb34";
var query2Neo = ("CREATE (p:Person { Name:'" + Name + "'})");

Are there another Way to avoid those kind of problems?

Best regards

1 ACCEPTED SOLUTION

paulare
Graph Buddy

Hi @CodeCase

You don't have to use string building - read cypher manual:
https://neo4j.com/docs/cypher-manual/4.0/syntax/parameters/

CREATE (n:Person { name: $name })

View solution in original post

6 REPLIES 6

paulare
Graph Buddy

Hi @CodeCase

You don't have to use string building - read cypher manual:
https://neo4j.com/docs/cypher-manual/4.0/syntax/parameters/

CREATE (n:Person { name: $name })

Hi @paul.are,

thanks for your reply.
I need to use Neo4j.Driver v4 with the following Syntax

 IDriver driver = GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.Basic("neo4j", "neo4j"));
 IAsyncSession session = driver.AsyncSession(o => o.WithDatabase("neo4j"));
 var name = "ÖöÜüÄäßµ@€-&'.Sonderbör";
 var query2Neo = ("CREATE (p:Person {name: $name})", new { name });
 IResultCursor cursor = await session.RunAsync(query2Neo.ToString());
 await cursor.ConsumeAsync().ConfigureAwait(true);
 await session.CloseAsync();
 await driver.CloseAsync();

dont know if thats the "best" approach for using the .Net Driver in neo4j. However its not working.neo4j

I get the following Error:

EXCEPTION: Neo4j.Driver.ClientException: Invalid input '(': expected (line 1, column 1 (offset: 0))
"(CREATE (p:Person {name: $name}), { name = ÖöÜüÄäßµ@€-&'.Sonderbör })"
^
at Neo4j.Driver.Internal.MessageHandling.ResponsePipelineError.EnsureThrownIf(Func`2 predicate)

Try this -

var name = "ÖöÜüÄäßµ@€-&'.Sonderbör";*
var query2Neo = "CREATE (p:Person {Name: $name})";
IResultCursor cursor = await session.RunAsync(query2Neo.ToString() , new { name });

Before this you need to create a param object for this to work.
I have tried with a java equivalent code and it works. - use only related part

Map<String, Object> parameters = new HashMap<String, Object>();
String name = "ÖöÜüÄäßµ@€-&'.Sonderbör";
String query2Neo = "CREATE (p:Person {Name: $name})";
parameters.put("name", name);
session.run(query2Neo, parameters);

Please follow this and if you still get the issue, please let me know

Hi @Kailash & @paul.are , thank you for your examples that helps me to understand what Im doing here

It works!

Kailash
Graph Buddy

Hi @CodeCase - as @paul.are mentioned , the string concatenation is not the effective way of creating nodes now. Instead use the parameter.
For some reason if you have to use it then wrap it around double quote ( " " ) instread of ( ' ')

Kailash
Graph Buddy

Thanks , Happy to help!