简体   繁体   English

插入数组时H2数据库中的SQL语法错误

[英]SQL syntax error in H2 Database when inserting array

I am actually trying to insert the data in the H2 database.我实际上是在尝试将数据插入到 H2 数据库中。 While starting up the application server, was getting the SQL syntax error exception.在启动应用服务器时,得到 SQL 语法错误异常。 I am really not sure if H2 database supports the insertion of array in the column?我真的不确定H2数据库是否支持在列中插入数组? Is there any issue with the below sql statement?下面的sql语句有问题吗? Does H2 database support any of the array datatypes float[], String[]...? H2 数据库是否支持任何数组数据类型 float[]、String[]...?

INSERT INTO weather (id,date,temperature) values ('1','2019-09-11','{"37.3","36.8","36.4"}');

CREATE TABLE WEATHER(
id INT AUTO_INCREMENT PRIMARY KEY,
date DATE,
temperature text[]
);
  1. You can't use PostgreSQL-style text[] as a data type in H2 and in other databases.您不能在 H2 和其他数据库中使用 PostgreSQL 样式的text[]作为数据类型。 H2 has the ARRAY data type for arrays: H2 具有ARRAY数据类型:

    https://h2database.com/html/datatypes.html#array_type https://h2database.com/html/datatypes.html#array_type

    H2 1.4.201 will also support standard-compliant array data type with a component type: H2 1.4.201 还将支持具有组件类型的符合标准的数组数据类型:

    componentDataType ARRAY[maximumCardinality]

    You can build H2 from its current sources if you really need that functionality right now, but I think you don't really need it, non-standard plain ARRAY will work too.如果您现在确实需要该功能,则可以从其当前来源构建 H2,但我认为您并不真正需要它,非标准普通ARRAY也可以使用。

  2. '{"37.3","36.8","36.4"}' is a character string literal. '{"37.3","36.8","36.4"}'是字符串文字。 H2 uses the standard array literals: H2 使用标准数组字面量:

    ARRAY[element, …]

    https://h2database.com/html/grammar.html#array https://h2database.com/html/grammar.html#array

    If you use some outdated version of H2 you need to use non-standard (element, …) literal instead (but don't use that variant in recent versions, it will be parsed by them as a row value as required by the Standard).如果您使用一些过时的 H2 版本,则需要使用非标准的(element, …)文字(但不要在最新版本中使用该变体,它们将根据标准的要求将其解析为行值) .

  3. It's not related with your question, but you really should use 1 instead of '1' as integer literal and DATE '2019-09-11' instead of '2019-09-11' as a date literal to avoid conversions from character strings to other data types.它与您的问题无关,但您确实应该使用1而不是'1'作为整数文字,使用DATE '2019-09-11'而不是'2019-09-11'作为日期文字,以避免从字符串转换为其他数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM