gpt4 book ai didi

scala - 执行更新和执行插入之间的 Anorm Scala 区别

转载 作者:行者123 更新时间:2023-12-01 02:26:24 25 4
gpt4 key购买 nike

我一直在思考使用 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(Cannot convert 2013-04-15 13:58:46.0:class java.sql.Timestamp to Long for column ColumnName(density_cache.day_of,Some(day_of)))

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

最佳答案

不同的是executeInsert将返回自动生成的 key (如果有)。

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使用正确的类型。

关于scala - 执行更新和执行插入之间的 Anorm Scala 区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16024916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com