加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Anorm Scala executeUpdate和executeInsert之间的区别

发布时间:2020-12-16 08:48:31 所属栏目:安全 来源:网络整理
导读:我一直在讨论使用executeUpdate()而不是executeInsert()之间的区别. 在下面的代码中,我使用了executeInsert() def addEntry(day: DateMidnight,create_time: DateTime,points: Long,src: String) = DB.withTransaction { implicit connection = Logger.debug
我一直在讨论使用executeUpdate()而不是executeInsert()之间的区别.

在下面的代码中,我使用了executeInsert()

def addEntry(day: DateMidnight,create_time: DateTime,points: Long,src: String) = DB.withTransaction { implicit connection => 

    Logger.debug("I got here")
    SQL(
      """
        INSERT INTO density_cache(day_of,create_time,points,src) 
           VALUES ({day_of},{create_time},{points},{src})
      """
    ).on(
      'day_of       -> day,'create_time  -> create_time,'points       -> points,'src          -> src
    ).executeInsert()
    Logger.debug("Got to 2nd step")
}

我遇到以下问题:
Java.lang.RuntimeException:TypeDoesNotMatch(无法将2013-04-15 13:58:46.0??:类java.sql.Timestamp转换为Long为列ColumnName(density_cache.day_of,Some(day_of)))

但是当我切换到executeUpdate()时,它工作正常.

解决方法

不同之处在于executeInsert将返回自动生成的密钥(如果有的话).

Anorm,simple SQL data access => Executing SQL queries

If you are inserting data that has an auto-generated Long primary key,you can call executeInsert(). If you have more than one generated key,or it is not a Long,executeInsert can be passed a ResultSetParser to return the correct key.

在你的情况下,我猜/假设你没有自动递增的主键,因此它不能与executeInsert()一起使用.如果您有,那么您可能必须传递具有正确类型的ResultSetParser.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读