SQL exception preparing query with ORMLite

后端 未结 4 418
一向
一向 2021-01-15 05:58

I am using an ORM (ORMlite) and all my calls are going well until I get the following error.

Exception in thread \"main\" org.h2.jdbc.JdbcSQLExceptio

相关标签:
4条回答
  • 2021-01-15 06:38

    I'm kind of guessing but it looks like there's a problem with the value in the title field, maybe an unescaped quote mark?

    I'm not familiar with ORMLite but title = 'Deepcut case leads 'not followed'' doesn't look right. Should probably be "Deepcut case leads 'not followed'" or 'Deepcut case leads \'not followed\'' or some such.

    0 讨论(0)
  • 2021-01-15 06:42

    Syntax error in SQL statement " SELECT * FROM ""STORIES"" WHERE ""TITLE""...

    @bemace is correct that there seem to be quotes in the title that is screwing up the escaping of strings generated by the query.

    In ORMLite, you should use the SelectArg feature which will generate a query with SQL ? arguments and then pass the string to the prepared statement directly.

    For documentation on the SelectArg, see:

    http://ormlite.com/docs/select-arg

    With SelectArg, you'd do something like:

    QueryBuilder<Story, Integer> queryBuilder = StoryDao.queryBuilder();
    SelectArg titleArg = new SelectArg();
    queryBuilder.where().eq(Story.TITLE_FIELD_NAME, titleArg);
    PreparedQuery<Story> preparedQuery = queryBuilder.prepare();
    titleArg.setValue(title);
    List<Story> accountList = StoryDao.query(preparedQuery);
    
    0 讨论(0)
  • 2021-01-15 06:46

    The correct syntax for the statement would be:

    SELECT * FROM Stories WHERE title = 'Deepcut case leads ''not followed'' ';
    

    Note the duplicated single quotes inside the string literal.

    You will need to tell your ORM layer to follow the ANSI SQL rules for literals.

    0 讨论(0)
  • 2021-01-15 06:57

    The exception says that there is some syntactical problem with your generated SELECT statement. Can you print out the generated query? Doing that might help you pin down the exact issue here.

    EDIT: Looking closely at your trace shows that string escaping is not handled properly here. Is this your own QueryBuilder? Also, as per this link, are you using SelectArg or directly setting the title?

    0 讨论(0)
提交回复
热议问题