我需要在查询构建器的having子句中使用占位符。
我试过用“?”标记但我得到:
java.sql.SQLException: argument holder index 1 not valid, only 1 in statement
这是我试图完成的事情:
QueryBuilder<ArchiefTag, Integer> archiefTagQb = helper
.getArchiefTagDao().queryBuilder();
tagSelectArg = new SelectArg();
archiefTagQb.where().in(ArchiefTag.TAG_FIELD, tagSelectArg);
archiefTagQb.groupBy(ArchiefTag.ARCHIEF_ENTRY_FIELD);
QueryBuilder<ArchiefEntry, Date> archiefEntryQb = helper
.getArchiefEntryDao().queryBuilder();
archiefEntryQb.join(archiefTagQb);
//the having must be on the outer join query. Don't know why
archiefEntryQb.having(String.format("COUNT(%s) = ?",ArchiefTag.TAG_FIELD));
preparedGetArchiefForTags = archiefEntryQb.prepare();
生成的sql是:
SELECT `ARCHIEF_ENTRY`.*
FROM `ARCHIEF_ENTRY`
INNER JOIN `ARCHIEF_TAG` ON `ARCHIEF_ENTRY`.`id_entry` = `ARCHIEF_TAG`.`entry`
GROUP BY `ARCHIEF_TAG`.`entry`
HAVING COUNT(tag) = ?
但是当我打电话的时候:
preparedGetArchiefForTags.setArgumentHolderValue(1, 3);
上述异常发生。
任何想法如何解决这个问题?
答案 0 :(得分:0)
但是当我打电话时:preparedGetArchiefForTags.setArgumentHolderValue(1,3);以上例外。
如果您查看javadocs for the setArgumentHolderValue(...)
method,index
参数应该基于0,而不 1基于SQL。
引用:
index - 您要设置的持有者的索引,基于0。见上面的注释。
所以你的代码应该是:
preparedGetArchiefForTags.setArgumentHolderValue(0, 3);
我已经调整了异常以使其更加清晰。
throw new SQLException("argument holder index " + index + " is not valid, only "
+ argHolders.length + " in statement (index starts at 0)");