【问题标题】:Insert Interval data using jooq使用 jooq 插入区间数据
【发布时间】:2020-10-05 21:21:47
【问题描述】:

我有一个 PostgreSQL 表,其列名称为 test_interval,类型为 interval。

test_interval interval

我正在使用 jooq 在表中插入数据。在我的 TestTable.java 类中,该字段的类型为:

public final TableField<TestTable, YearToSecond> TEST_INTERVAL = createField(DSL.name("test_interval"), org.jooq.impl.SQLDataType.INTERVAL.nullable(false), this, "");

我将表格创建为:

dsl.createTableIfNotExists(TestTable)
   .column(TestTable.TEST_INTERVAL)
   .execute()  

我想往表中插入数据,数据的形式是:

val str = "0 years 0 mons 0 days 0 hours 15 mins 0.00 secs"   
dsl.insertInto(
    TestTable,
    TestTable.TEST_INTERVAL
).values(
    str
)

我得到错误无法插入类型。如何使用jooq插入postgres区间数据类型的数据?

【问题讨论】:

    标签: java postgresql jooq querydsl jooq-sbt-plugin


    【解决方案1】:

    您的TEST_INTERVAL 列属于YearToSecond 类型,因此您必须插入该类型的值,而不是String 类型的值。

    只需拨打YearToSecond constructor

    new YearToSecond(
      new YearToMonth(),
      new DayToSecond(0, 0, 15)
    )
    

    【讨论】:

    • 我做了 YearToSecond.valueOf(str) where str = "0 years 0 mons 0 days 0 hours 15 mins 0.00 secs" 对我不起作用@LukasEder
    • 是的,这是一种 PostgreSQL 特定格式,该方法不支持。为什么不直接调用具有特定值的构造函数呢?请参阅我的更新答案。
    猜你喜欢
    • 2019-10-19
    • 2021-03-02
    • 2016-09-01
    • 2019-03-02
    • 2018-01-15
    • 2015-11-16
    • 2017-09-08
    • 2018-10-03
    • 2021-08-01
    相关资源
    最近更新 更多