Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Scala anorm检索插入的id_Mysql_Scala_Playframework 2.0_Anorm_Typesafe Stack - Fatal编程技术网

Mysql Scala anorm检索插入的id

Mysql Scala anorm检索插入的id,mysql,scala,playframework-2.0,anorm,typesafe-stack,Mysql,Scala,Playframework 2.0,Anorm,Typesafe Stack,我有一个id字段为autoid的表。在插入带有anorm的行后,我想检索生成的id。知道吗 SQL( """ insert into accom (id,places,lat,lon,permaname,country,defaultLanguage) values ( {places}, {lat}, {lon}, {permaname}, {country}, {defaultLanguage} ) """).on( '

我有一个id字段为autoid的表。在插入带有anorm的行后,我想检索生成的id。知道吗

 SQL(
    """
      insert into accom (id,places,lat,lon,permaname,country,defaultLanguage) values (
         {places}, {lat}, {lon}, {permaname}, {country}, {defaultLanguage}
      )
    """).on(
      'id -> id,
      'places -> places,
      'lat -> lat,
      'lon -> lon,
      'permaname -> permaname,
      'country -> country,
      'defaultLanguage -> defaultLanguage).executeUpdate()
}

使用
executeInsert
而不是
executeUpdate
,返回值是id。

使用
executeInsert
方法而不是
executeUpdate
;它返回一个
选项[T]
,其中
T
是主键的类型


您可能想看一看,它还向您展示了如何在不指定要生成的id的情况下制定
INSERT
语句

在最新版本中,您需要标量:

   val newId = SQL(
      """
      insert into accom (places,lat,lon,permaname,country,defaultLanguage) 
      values ({places}, {lat}, {lon}, {permaname}, {country}, {defaultLanguage})
      """).on(
        'places -> places,
        'lat -> lat,
        'lon -> lon,
        'permaname -> permaname,
        'country -> country,
        'defaultLanguage -> defaultLanguage).executeInsert(scalar[Long].single)  

这是正确的答案。如果不使用标量参数,我会看到奇怪的列类型不匹配错误。