postgresql, pgobject可用类型,数组类型

时间:2022-01-15 16:24:59

I use postgresql 9.5 with jdbc driver 9.4.1208.jre7 and scalikejdbc wrapper

我使用postgresql 9.5和jdbc驱动程序9.4.1208。jre7和scalikejdbc包装

My table is:

我的表是:

CREATE TABLE refs
(
  name character varying NOT NULL,
  custom json,
  prices integer[]
)

I can insert json values using org.postgresql.util.PGobject:

我可以使用org. postgrel .util. pgobject:

val res = new PGobject()
res.setType("json")
res.setValue("{'name': 'test'}")
res

I also want to insert arrays. How can i do this? I thought that this would work:

我还想插入数组。我该怎么做呢?我认为这行得通:

  def toArrStr(a: Iterable[String]): PGobject = {
    val res = new PGobject()
    res.setType("ARRAY")
    res.setValue(s"{${a.map(i => '"' + i + '"').mkString(",")}}")
    res
  }

But it gives me exception: org.postgresql.util.PSQLException: Unknown type ARRAY

但它给了我一个例外:org.postgresql.util。PSQLException:未知类型数组

May be i'm missing smth but i can't find good docs about PGObject class. I think that PGObject class was designed exactly for purposes like mine but it doesn't behaves as expected

可能我错过了smth,但是我找不到关于PGObject类的好文档。我认为PGObject类的设计与我的目的完全相同,但它的行为并不像预期的那样

POSTGRES has many types, not only array but date, datetime, daterange, timestamprange, so on. I believe that there should be type names for its corresponding types.

POSTGRES有很多类型,不仅是数组,还包括日期、datetime、daterange、timestamprange等。我认为应该有相应类型的类型名称。

1 个解决方案

#1


1  

I understood how to save character list[] using PGObject:

我知道如何使用PGObject保存字符列表[]:

  def toArrStr(a: Iterable[String]): PGobject = {
    val res = new PGobject()
    res.setType("varchar[]")
    res.setValue(s"{${a.map(i => '"' + i + '"').mkString(",")}}")
    res
  }

To save array of numbers: where size is 2,4,8 (smallint, int, bigint)

保存数字数组:其中大小为2,4,8 (smallint, int, bigint)

  def toArrNum(a: Iterable[AnyVal], size: Int): PGobject = {
    val res = new PGobject()
    res.setType(s"int$size[]")
    res.setValue(s"{${a.mkString(",")}}")
    res
  }

#1


1  

I understood how to save character list[] using PGObject:

我知道如何使用PGObject保存字符列表[]:

  def toArrStr(a: Iterable[String]): PGobject = {
    val res = new PGobject()
    res.setType("varchar[]")
    res.setValue(s"{${a.map(i => '"' + i + '"').mkString(",")}}")
    res
  }

To save array of numbers: where size is 2,4,8 (smallint, int, bigint)

保存数字数组:其中大小为2,4,8 (smallint, int, bigint)

  def toArrNum(a: Iterable[AnyVal], size: Int): PGobject = {
    val res = new PGobject()
    res.setType(s"int$size[]")
    res.setValue(s"{${a.mkString(",")}}")
    res
  }