加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – 如何使用ScalaQuery插入BLOB字段?

发布时间:2020-12-12 08:55:06 所属栏目:MsSql教程 来源:网络整理
导读:我使用了 ScalaQuery和Scala. 如果我有一个Array [Byte]对象,我该如何将它插入表中? object TestTable extends BasicTable[Test]("test") { def id = column[Long]("mid",O.NotNull) def extInfo = column[Blob]("mbody",O.Nullable) def * = id ~ extInfo (
我使用了 ScalaQuery和Scala.

如果我有一个Array [Byte]对象,我该如何将它插入表中?

object TestTable extends BasicTable[Test]("test") {
  def id = column[Long]("mid",O.NotNull)
  def extInfo = column[Blob]("mbody",O.Nullable)

  def * = id ~ extInfo <> (Test,Test.unapply _)
}

case class Test(id: Long,extInfo: Blob)

我可以定义使用def extInfo = column [Array [Byte]](“mbody”,O.Nullable)的方法,如何使用BLOB类型字段进行操作(UPDATE,INSERT,SELECT)?

BTW:没有ScalaQuery标签

解决方法

由于BLOB字段可以为空,我建议将其Scala类型更改为Option [Blob],用于以下定义:
object TestTable extends Table[Test]("test") {
  def id = column[Long]("mid")
  def extInfo = column[Option[Blob]]("mbody")
  def * = id ~ extInfo <> (Test,extInfo: Option[Blob])

如果您愿意,可以使用原始的,可以为空的Blob值,但是您需要在列上使用orElse(null)来实际获取空值(而不是抛出异常):

def * = id ~ extInfo.orElse(null) <> (Test,Test.unapply _)

现在进行实际的BLOB处理.读取是直截了当的:您只需在JDBC驱动程序实现的结果中获取Blob对象,例如:

Query(TestTable) foreach { t =>
    println("mid=" + t.id + ",mbody = " +
      Option(t.extInfo).map { b => b.getBytes(1,b.length.toInt).mkString })
  }

如果要插入或更新数据,则需要创建自己的BLOB. JDBC的RowSet功能提供了适用于独立Blob对象的实现:

import javax.sql.rowset.serial.SerialBlob

TestTable insert Test(1,null)
TestTable insert Test(2,new SerialBlob(Array[Byte](1,2,3)))

编辑:这是Postgres的一个TypeMapper [Array [Byte]](ScalaQuery尚不支持BLOB):

implicit object PostgresByteArrayTypeMapper extends
      BaseTypeMapper[Array[Byte]] with TypeMapperDelegate[Array[Byte]] {
    def apply(p: BasicProfile) = this
    val zero = new Array[Byte](0)
    val sqlType = java.sql.Types.BLOB
    override val sqlTypeName = "BYTEA"
    def setValue(v: Array[Byte],p: PositionedParameters) {
      p.pos += 1
      p.ps.setBytes(p.pos,v)
    }
    def setOption(v: Option[Array[Byte]],p: PositionedParameters) {
      p.pos += 1
      if(v eq None) p.ps.setBytes(p.pos,null) else p.ps.setBytes(p.pos,v.get)
    }
    def nextValue(r: PositionedResult) = {
      r.pos += 1
      r.rs.getBytes(r.pos)
    }
    def updateValue(v: Array[Byte],r: PositionedResult) {
      r.pos += 1
      r.rs.updateBytes(r.pos,v)
    }
    override def valueToSQLLiteral(value: Array[Byte]) =
      throw new SQueryException("Cannot convert BYTEA to literal")
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读