Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3282788
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:58:02+00:00 2026-05-17T19:58:02+00:00

I used ScalaQuery and Scala. If I have an Array[Byte] object, how do I

  • 0

I used ScalaQuery and Scala.

If I have an Array[Byte] object, how do I insert it into the table?

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)

Can I define the method used def extInfo = column[Array[Byte]]("mbody", O.Nullable), how to operate(UPDATE, INSERT, SELECT) with the BLOB type field?

BTW: no ScalaQuery tag

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:58:03+00:00Added an answer on May 17, 2026 at 7:58 pm

    Since the BLOB field is nullable, I suggest changing its Scala type to Option[Blob], for the following definition:

    object TestTable extends Table[Test]("test") {
      def id = column[Long]("mid")
      def extInfo = column[Option[Blob]]("mbody")
      def * = id ~ extInfo <> (Test, Test.unapply _)
    }
    
    case class Test(id: Long, extInfo: Option[Blob])
    

    You can use a raw, nullable Blob value if you prefer, but then you need to use orElse(null) on the column to actually get a null value out of it (instead of throwing an Exception):

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

    Now for the actual BLOB handling. Reading is straight-forward: You just get a Blob object in the result which is implemented by the JDBC driver, e.g.:

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

    If you want to insert or update data, you need to create your own BLOBs. A suitable implementation for a stand-alone Blob object is provided by JDBC’s RowSet feature:

    import javax.sql.rowset.serial.SerialBlob
    
    TestTable insert Test(1, null)
    TestTable insert Test(2, new SerialBlob(Array[Byte](1,2,3)))
    

    Edit: And here’s a TypeMapper[Array[Byte]] for Postgres (whose BLOBs are not yet supported by ScalaQuery):

      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")
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Used as a scripting language, does Scala have some sort of include directive, or
I used the LINQ to SQL designer in Visual Studio to create an object
Used to develop Portlets and JPS's, so have a dilemma. task: display <h:dataTable> only
We used the undocumented xp_fileexist stored procedure for years in SQL Server 2000 and
Never used a cache like this before. The problem is that I want to
I used to work in a place where a common practice was to use
I used jQuery to set hover callbacks for elements on my page. I'm now
When used like this: import static com.showboy.Myclass; public class Anotherclass{} what's the difference between
We used to use SourceSafe, and one thing I liked about it was that
I used to be able to do the following in Preview 3 <%=Html.BuildUrlFromExpression<AController>(c =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.