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

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

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

  1. yyyy-MM-dd-hh.mm.ss

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

此外,我正在使用:

  1. <dependency>
  2. <groupId>org.flywaydb</groupId>
  3. <artifactId>flyway-core</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.h2database</groupId>
  7. <artifactId>h2</artifactId>
  8. <scope>runtime</scope>
  9. </dependency>

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

  1. spring.h2.console.enabled=true
  2. spring.h2.console.path=/h2
  3. spring.datasource.url=jdbc:h2:mem:~/capitole
  4. spring.datasource=capitole
  5. spring.datasource.username=sa
  6. spring.datasource.password=
  7. spring.datasource.driver-class-name=org.h2.Driver
  8. spring.datasource.generate-unique-name=false
  9. # This is for FlyWay configuration
  10. spring.flyway.url=jdbc:h2:mem:~/capitole
  11. spring.flyway.schemas=capitole
  12. spring.flyway.user=sa
  13. spring.flyway.password=

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

  1. drop table if exists PRICES;
  2. create table PRICES (
  3. Id int not null AUTO_INCREMENT,
  4. brand_id int not null,
  5. start_date TIMESTAMP not null,
  6. end_date timestamp not null,
  7. price_list int not null,
  8. product_id int not null,
  9. priority int not null,
  10. price double not null,
  11. curr varchar(50) not null
  12. );

第二个迁移是插入:

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

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

dgtucam1

dgtucam11#

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

  1. 2020-06-14 00:00:00

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

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

相关问题