2013-04-15 37 views
4

我一直在使用executeUpdate()而不是executeInsert()Anorm Scala 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年4月15日13:58:46.0​​:級Java。 sql.Timestamp到長列ColumnName(density_cache.day_of,一些(day_of)))

但是,當我切換到executeUpdate(),它工作正常。

回答

3

區別在於executeInsert將返回自動生成的密鑰(如果有的話)。

Anorm, simple SQL data access => Executing SQL queries

如果插入具有一個自動生成的Long主鍵的數據,你可以調用executeInsert()。如果您有多個生成的密鑰,或者它不是Long,則executeInsert可以通過ResultSetParser返回正確的密鑰。

在你的情況我猜/假設你沒有一個自動遞增的主鍵,因此它不會與executeInsert()一起使用。如果你有,那麼你可能必須通過ResultSetParser正確的類型。