Anorm Scala Difference between executeUpdate and executeInsert

I waved my difference between using executeUpdate() rather than executeInsert() .

In the following code, I used 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") } 

I get the following problem: 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)))

But when I switch to executeUpdate() , it works fine.

+4
source share
1 answer

The difference is that executeInsert will return the automatically generated key (if any).

Anorm, easy access to SQL data => Executing SQL queries

If you insert data that has an automatically generated Long primary key, you can call executeInsert() . If you have several generated keys or it is not long, executeInsert can be passed to ResultSetParser to return the correct key.

In your case, I assume / assume that you do not have an automatically generated primary key, and therefore it will not work with executeInsert() . If you have, you may need to pass ResultSetParser with the correct type.

+3
source

All Articles