简体   繁体   English

PostgreSQL:错误:数组下标超出范围

[英]PostgreSQL: ERROR: array subscript out of range

I am trying to fill two dimensional array我正在尝试填充二维数组

do $$

declare pole text[][];

begin
for y in 1..6
loop
  for x in 1..4
  loop
    pole[y][x] = '0';
    raise notice 'x: %',x;
    raise notice 'y: %',y;
  end loop;
end loop;

/*
pole  := '{
{0,0,0,0}, 
{7,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}
}';
*/

raise notice 'pole : %', pole;
raise notice 'pole one: %', pole[2][1];

end $$

but getting ERROR: array subscript out of range , If I fill the array manually like pole[6][4] := '0' it has no problem, but once the loop is used I get error, I don't know why, variables are between 1 and 4 and 1 and 6 and manual assignment works.但是得到ERROR: array subscript out of range ,如果我像pole[6][4] := '0'这样手动填充数组就没有问题,但是一旦使用循环我就会出错,我不知道为什么, 变量介于 1 和 4 和 1 和 6 之间,手动分配有效。 This is basic programming am I missing something?这是基本编程我错过了什么吗?

1) SQL State: 00000 --- x: 1
2) SQL State: 00000 --- y: 1
3) SQL State: 00000 --- x: 2
4) SQL State: 00000 --- y: 1
5) SQL State: 00000 --- x: 3
6) SQL State: 00000 --- y: 1
7) SQL State: 00000 --- x: 4
8) SQL State: 00000 --- y: 1
9) SQL State: 00000 --- x: 1
10) SQL State: 00000 --- y: 2
11) SQL State: 00000 --- x: 2
12) SQL State: 00000 --- y: 2
13) SQL State: 00000 --- x: 3
14) SQL State: 00000 --- y: 2
15) SQL State: 00000 --- x: 4
16) SQL State: 00000 --- y: 2
17) SQL State: 00000 --- x: 1
18) SQL State: 00000 --- y: 3
19) SQL State: 00000 --- x: 2
20) SQL State: 00000 --- y: 3
21) SQL State: 00000 --- x: 3
22) SQL State: 00000 --- y: 3
23) SQL State: 00000 --- x: 4
24) SQL State: 00000 --- y: 3
25) SQL State: 00000 --- x: 1
26) SQL State: 00000 --- y: 4
27) SQL State: 00000 --- x: 2
28) SQL State: 00000 --- y: 4
29) SQL State: 00000 --- x: 3
30) SQL State: 00000 --- y: 4
31) SQL State: 00000 --- x: 4
32) SQL State: 00000 --- y: 4
33) SQL State: 00000 --- x: 1
34) SQL State: 00000 --- y: 5
35) SQL State: 00000 --- x: 2
36) SQL State: 00000 --- y: 5
37) SQL State: 00000 --- x: 3
38) SQL State: 00000 --- y: 5
39) SQL State: 00000 --- x: 4
40) SQL State: 00000 --- y: 5
41) SQL State: 00000 --- x: 1
42) SQL State: 00000 --- y: 6
43) SQL State: 00000 --- x: 2
44) SQL State: 00000 --- y: 6
45) SQL State: 00000 --- x: 3
46) SQL State: 00000 --- y: 6
47) SQL State: 00000 --- x: 4
48) SQL State: 00000 --- y: 6

PostgreSQL 11.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit x86_64-pc-linux-gnu 上的 PostgreSQL 11.6,由 gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39) 编译,64 位

Multidimensional array cannot grow like 1D arrays多维数组不能像一维数组一样增长

A stored array value can be enlarged by assigning to elements not already present.可以通过分配给不存在的元素来扩大存储的数组值。 Any positions between those previously present and the newly assigned elements will be filled with nulls.先前存在的元素和新分配的元素之间的任何位置都将填充空值。 For example, if array myarray currently has 4 elements, it will have six elements after an update that assigns to myarray[6];例如,如果数组 myarray 当前有 4 个元素,则在分配给 myarray[6] 的更新后它将有 6 个元素; myarray[5] will contain null. myarray[5] 将包含空值。 Currently, enlargement in this fashion is only allowed for one-dimensional arrays, not multidimensional arrays.目前,这种方式的放大只允许用于一维数组,而不允许用于多维数组。

So you would have to initialize the array first, then populate it.所以你必须先初始化数组,然后填充它。

do $$

declare pole text[][];

begin

pole := array_fill(null::text, array[6,4]);

for y in 1..6
loop
  for x in 1..4
  loop

    raise notice 'x: %',x;
    raise notice 'y: %',y;
    pole[y][x] = '0';
  end loop;
end loop;


raise notice 'pole : %', pole;
raise notice 'pole one: %', pole[2][1];

end $$;

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

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