How to know that if a Insert query was succesfull in anorm?

Shi*_*wal 6 sql scala playframework anorm

我在Play应用程序中使用Anorm进行数据库查询.我经历了一些教程,如果执行成功SQL(....).execute()则会返回Boolean.我测试了该方法,但它总是返回false(不知道它什么时候返回true:/).我也试过,SQL(...).executeInsert()但表中没有任何"自动增量"列,所以问题仍然存在.如果有任何解决方案(任何扩展版本的'.execute()'方法或其他),请帮助我.

这是我的代码的一部分,由于意外返回而失败...

    def addSuggestion(sessionId: BigInteger, suggestionId: BigInteger) = {
        DB.withConnection { implicit c =>
          if (!SQL("insert into user_suggestion_" + sessionId + " values (" + suggestionId + ",1,0,0)").execute()) {
            SQL("update user_suggestion_" + sessionId + " set count=(count+1) where user_id=" + suggestionId).executeUpdate()
          }
        }
      }
Run Code Online (Sandbox Code Playgroud)

更新查询应仅在插入失败时运行(由于任何约束等).还有其他功能/替代方案吗?请帮忙.提前致谢.

joh*_*ren 7

Anorm调用.execute()委托给jdbc PreparedStatement类的.execute(),如果结果是ResultSet则返回true,如果是"更新计数"则返回false,或者没有结果返回,所以它不会你期望它.

http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#execute()

只要execute()调用没有抛出SqlException,我希望插入成功.(您可以通过尝试插入具有表中已有ID的条目来轻松验证这一点)