【发布时间】:2011-06-24 12:08:00
【问题描述】:
我是 grails 的新手,我尝试获取数据库条目。 数据库连接正常工作。
在数据库中,我有一个名为“Correct”的类,其中包含正确答案(布尔值),所以它的结构是:
标识 |版本 |回答 | ... |回答 |脂酸
lpicid 对问题的引用。对于每个 lpicid,只有一个条目。主键是 id。
现在当我想调用正确答案并将答案保存在变量中时,这实际上不起作用:
def correctanswers = Correct.executeQuery("from Correct answers where answers.lpicid=" + lpicid);
def correcta = correctanswers.answera();
我得到一个数据库条目,我可以看到(lpicid 只是一个 def 数字),但是 correctanswers.answera() 不起作用。我总是收到这条消息:
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.answera() is applicable for argument types: () values: []
Possible solutions: inspect(), clear(), clear(), clear(), any(), asList()
at com.lpic.LpicSimulatorController$_closure2.doCall(LpicSimulatorController.groovy:76)
at com.lpic.LpicSimulatorController$_closure2.doCall(LpicSimulatorController.groovy)
at java.lang.Thread.run(Thread.java:662)
我该如何解决这个问题以获得 answera、answerb.... 值?
谢谢你:-)
【问题讨论】:
-
您正在使用的字符串连接非常危险,并且可能允许 SQL 注入攻击。虽然
get()是正确答案,但一般Correct.executeQuery("from Correct answers where answers.lpicid=" + lpicid)应该是Correct.executeQuery("from Correct answers where answers.lpicid=?" , [lpicid])或Correct.executeQuery("from Correct answers where answers.lpicid=:id", [id: lpicid])。请参阅grails.org/doc/latest/ref/Domain%20Classes/executeQuery.html 上的文档
标签: database hibernate grails groovy hql