Spring Boot 如何将值插入到格式为www.example.com的H2 DB时间戳列中yyyy-MM-dd-hh.mm.ss?

xt0899hw  于 2023-03-02  发布在  Spring
关注(0)|答案(1)|浏览(85)

我正在尝试使用Flyway迁移插入到H2数据库的时间戳列中。我的目标是用以下格式保存日期:

yyyy-MM-dd-hh.mm.ss

但我收到了这个错误消息:

此外,我正在使用:

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
</dependency>

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

此外,我有应用程序属性文件如下:

spring.h2.console.enabled=true
spring.h2.console.path=/h2

spring.datasource.url=jdbc:h2:mem:~/capitole
spring.datasource=capitole
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.generate-unique-name=false

# This is for FlyWay configuration
spring.flyway.url=jdbc:h2:mem:~/capitole
spring.flyway.schemas=capitole
spring.flyway.user=sa
spring.flyway.password=

我用这种方法创建我的表:
V1_1__init.database.sql

drop table if exists PRICES;
create table PRICES (
    Id            int       not null AUTO_INCREMENT,
    brand_id      int       not null,
    start_date    TIMESTAMP   not null,
    end_date      timestamp  not null,
    price_list    int       not null,
    product_id    int       not null,
    priority      int       not null,
    price         double    not null,
    curr          varchar(50)   not null
);

第二个迁移是插入:

insert into PRICES(brand_id, start_date, end_date, price_list, product_id,priority,price,curr)
values (1,
        parsedatetime('2020-06-14-00.00.00','yyyy-MM-dd-hh.mm.ss'),
        parsedatetime('2020-12-31-23.59.59','yyyy-MM-dd-hh.mm.ss'),
        1,
        35455,
        0,
        35.50,
        'EUR');

因此,当我运行应用程序时,会收到前面显示的错误消息。
所以我希望你能帮我。谢谢。

dgtucam1

dgtucam11#

我建议“顺其自然”,使用支持的日期时间格式:

2020-06-14 00:00:00

从示例SQL文件中可以看出,您使用的是硬编码值,也就是说,您没有使用您提到的格式的SQL转储。在这种情况下,使用不同格式的工作量很小。
但是如果你使用了大量的时间戳,最好的方法是创建一个简单的实用工具来“解析”输入文件,重新格式化时间戳,并将其输出到一个输出文件,你可以使用\d{4}-\d{2}-\d{2}-\d{2}\.\d{2}\.\d{2}正则表达式来定位时间戳,并为每个时间戳调用这个方法:

String fixFormat(String timestamp) {
  return timestamp.substring(0, 10)
      + " "
      + timestamp.substring(11).replaceAll('.', ':');
}

相关问题