-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: createNativeQuery with Question Mark and Single Quote
PostPosted: Sat Oct 17, 2015 8:43 pm 
Newbie

Joined: Sat Oct 17, 2015 8:25 pm
Posts: 1
Hi, I'm having problem with a query. How to escape the Question Mark?
My example code, that should insert 2 rows on my table (books), is :

try
{
String values = "('Who are you? What''s your name?','Me'),"
+ "('Are you happy?','You')";
String sqlQuery = "INSERT INTO books "
+ "(TITLE, AUTHOR) VALUES "+values;
Query q = em.createNativeQuery(sqlQuery);
q.executeUpdate();
return "success";
}
catch (Exception ex)
{
return "error : "+ex.getMessage();
}

The reported error is :

error : Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Me'),('Are you happynull','You')' at line 1 Error Code: 1064 Call: INSERT INTO books (TITLE, AUTHOR) VALUES ('Who are you\? What''s your name?,'Me'),('Are you happy?','You') bind => [1 parameter bound] Query: DataModifyQuery(sql="INSERT INTO books (TITLE, AUTHOR) VALUES ('Who are you\? What''s your name?,'Me'),('Are you happy?','You')")

If I remove the single quote or the first question mark, the query is executed.
I need to execute the query without q.setParameter, because, as I said, it´s an example code. My final software will read an Excel with more than 10000 rows, do a loop through them, and build the values structure that will be passed by post, reducing the insert delay. If I used setParameter, I should to create a post in a json format, with a object (class) array, do a loop through them again, and insert one by one, it would cause me more then 20 seconds delay.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.