Postgres - 通过 SQL 插入一个 varchar 数组

Postgres - inserting a varchar array via SQL

我想执行这个查询。

INSERT INTO items (title, description,
start_date,
expiration_date,
type,
quantity,
state,
sold,
cover_photo_file_name,
cover_photo_content_type,
cover_photo_file_size,
instructions)
VALUES
(
    'some_test_value',
    'lorem ipsum',
    '2015-01-01 00:00:00',
    '2015-06-10 00:00:00',
    0,
    19,
    0,
    0,
    'RackMultipart20150317-10093-1ny4gmx.gif',
    'image/gif',
    157574,
    {"lorem ipsum\r","dolor sit\r","loremloremipsumipsum 'sitsit' dolor sit"}
    );

我很讨厌

ERROR: syntax error at or near "{"

当我尝试这样做时

ARRAY["Lorem ipsum\r", ...]

我得到:

ERROR: column "Lorem ipsum" does not exist

问题是我现在无法更改数据库(我没有时间)所以我需要构建一个查询来 "do" 作业(尽管这个数据库需要使用规范化删除数据库...)

我的代码有什么问题?也许我遗漏了一些明显的东西,但我疲惫的头脑只是跳过它。

字符串文字需要用单引号括起来'而不是双引号。

只有在使用 {...} 的 "short hand" 数组符号中,值需要用双引号引起来:

'{"lorem ipsum", "dolor sit"}'::text[]

在这里使用 ARRAY 构造函数更容易:

ARRAY['Lorem ipsum\r', ...]

因为单引号不需要转义。

INSERT INTO items (title, description,
start_date,
expiration_date,
type,
quantity,
state,
sold,
cover_photo_file_name,
cover_photo_content_type,
cover_photo_file_size,
instructions)
VALUES
  ('some_test_value',
  'lorem ipsum',
'2015-01-01 00:00:00',
'2015-06-10 00:00:00',
0,
19,
0,
0,
'RackMultipart20150317-10093-1ny4gmx.gif',
'image/gif',
157574,
'{"lorem ipsum\r","dolor sit\r","loremloremipsumipsum 'sitsit' dolor sit"}'
);

请试试这个...