如何正确使用 Scala Play Anorm 和 Option[String] 插入 NULL SQL [英] How to properly use Scala Play Anorm and Option[String] to insert NULL SQL

查看:70
本文介绍了如何正确使用 Scala Play Anorm 和 Option[String] 插入 NULL SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 Option[String] 为 None 时插入的正确方法是什么?下面的代码插入一个空字符串,与mysql中的NULL不同.

What is the proper way to insert Option[String] when it is None? The below code inserts an empty string, which is not the same as NULL in mysql.

是否只能根据partnerCode的内容预先构建SQL字符串?叹息...异常...

Is the only way to build the SQL string beforehand based on the content of partnerCode? Sigh...Anorm...

DB.withConnection { implicit connection =>

  val id: Option[Long] = SQL(
    """
    INSERT INTO users (email, partner_code, is_active, created, pass) VALUES ({email}, {partnerCode}, 0, NOW(), {pass})
    """
  ).on(
    'email -> user.email,
    'partnerCode -> user.partnerCode.getOrElse(""),  // FIXME: how to use NULL keyword instead of empty string? Is Anorm just this dumb?
    'pass -> hashPassword(user.password.get)
  ).executeInsert()

  id.get

}

推荐答案

None 如果您导入 anorm._anorm.toParameterValue<实际上应该可以正常工作/代码>:

None should actually work fine if you import anorm._ or anorm.toParameterValue:

'partnerCode -> user.partnerCode

这篇关于如何正确使用 Scala Play Anorm 和 Option[String] 插入 NULL SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆