简体   繁体   English

在播放框架2中使用anorm将对象持久化在postgresql数据库中

[英]persist object in postgresql database with anorm in a play framework 2

I'm trying to persist objects to a postgresql Db inside my play2 webapp using anorm library. 我正在尝试使用anorm库将对象持久保存到我的play2 webapp中的postgresql Db中。 The Create statement of the table "Address" is this one: 表“地址”的Create语句是这样的:

create table address (
    addressId               bigserial not null,
    addressName             varchar(255) not null,
    street                  varchar(255) not null,
    number                  varchar(15) not null,
    ZIP                     varchar(15) not null,
    region                  varchar(63) not null,
    country                 varchar(63) not null,
    constraint pk_address primary key (addressId)
);

And my Scala/Anorm code looks like this: 我的Scala / Anorm代码如下所示:

private def create(address: Address, recursive : Boolean): Long = {
    DB.withConnection { implicit connection =>
        SQL( """
             insert into {table_name} (addressName, street, number, ZIP, region, country)
             values ({name},{street},{number},{ZIP},{region},{country})
            """).on(
            "table_name" -> TABLE_NAME,
            "name" -> address.name,
            "street" -> address.street,
            "number" -> address.number,
            "ZIP" -> address.ZIP,
            "region" -> address.region,
            "country"-> address.country
        ).executeInsert[Option[Long]]().get
    }
}

Obviously TABLE_NAME is a val that contains the string "Address" 显然,TABLE_NAME是一个包含字符串“ Address”的值

The problem is that when i try to execute it with a proper Address instance like: 问题是当我尝试使用适当的Address实例执行它时:

Address("Antonio","vacchi","12","48012","RA","Italia")

I get this exception: 我得到这个例外:

[PSQLException: ERROR: syntax error at or near "$1" Posizione: 13] 

'Posizione' is the italian word for Position (dunno why i get the error in italian o:) “ Posizione”是“ Position”的意大利语词(不知道为什么我收到意大利语o的错误:)

Using the interpolation with the {table_name} syntax will result in the SQL containing a positioned parameter. {table_name}语法中使用插值将导致SQL包含一个定位参数。 This is something that is not supported when it comes to CREATE statements. 对于CREATE语句,这是不受支持的。 So in this case you have to write out Address and not pass it in the on parameters. 因此,在这种情况下,您必须写出Address而不是将其传递给on参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM