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.
|