小编典典

使用ORMLite进行SQL异常准备查询

sql

我正在使用ORM(ORMlite),并且所有通话都进展顺利,直到出现以下错误。

线程“主”中的异常org.h2.jdbc.JdbcSQLException:SQL语句“ SELECT * FROM”“ STORIES”“
WHERE”“ TITLE”“ =’Deepcut大小写导致’NOT FOLLOWED [*]”“; SQL语句:SELECT * FROM
StoriesWHEREtitle=’在org.h2.message.DbException.get(DbException.java:167)处org.h2.message.DbException.getJdbcSQLException(DbException.java:327)的’Deepcut
case导致’not follow’[42000-152]
org.h2.message.DbException.getSyntaxError(DbException.java:179)的org.h2.message.DbException.get(DbException.java:144)org.h2.command.Parser.getSyntaxError(Parser.java:480)的org.h2.message.DbException.getSyntaxError(DbException.java:179)在org.h2.engine.Session.prepareCommand(Session.java:374)在org.h2.engine.Session.prepareLocal(Session.java:426)的org.h2.command.Parser.prepareCommand(Parser.java:229)
)于org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.prepareCommand(JdbcConnection.java:1093)于org.h2.jdbc.JdbcPreparedStatement。(JdbcPreparedStatement.java:71)处。com.j256.ormlite.jdbc.JdbcDatabaseConnection.compileStatement(JdbcDatabaseConnection.java:83)处com.j256.ormlite.stmt.mapped.MappedPreparedStmt.compile(MappedPreparedStmt.java:44)处com.j256.ormlite
com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:119)上的com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:189)上的.stmt.StatementExecutor.buildIterator(StatementExecutor.java:169)
)119)com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:189)119)com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:189)

我对发生的事情感到困惑。我从这些行中调用搜索:

// get our query builder from the DAO
QueryBuilder<Story, Integer> queryBuilder = StoryDao.queryBuilder();
// the 'title' field must be equal to title (a variable)
queryBuilder.where().eq(Story.TITLE_FIELD_NAME, title);
// prepare our sql statement
PreparedQuery<Story> preparedQuery = queryBuilder.prepare();
// query for all stories that have that title
List<Story> accountList = StoryDao.query(preparedQuery);

阅读 354

收藏
2021-04-07

共1个答案

小编典典

SQL语句“ SELECT * FROM”“ STORIES”“ WHERE”“ TITLE”“中的语法错误…

@bemace是正确的,因为标题中似乎有引号正在加紧查询所生成的字符串的转义。

在ORMLite中,您应该使用该SelectArg功能,该功能将使用SQL生成查询?参数,然后将字符串直接传递给准备好的语句。

请参阅上的文档SelectArg。有了它,您将执行以下操作:

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);
2021-04-07