Issue for insert using psycopg

两盒软妹~` 提交于 2019-11-28 14:44:10

The INSERT statement has invalid syntax. There something wrong inside the for loop you mention.
You should include the for loop in the question.

INSERT INTO "DUMMY1km" (data) VALUES ([[-3000 -3000 -3000 .....

A valid statement could look like this - assuming your column is of type integer[].
... which you should also include in the question.

INSERT INTO "DUMMY1km"(data) VALUES ('{-3000, -3000}'::int[])

or

INSERT INTO "DUMMY1km"(data) VALUES (ARRAY[-3000, -3000])  -- note the "ARRAY"

or for a 2-dimensional array (looks a bit like that in the error msg.):

INSERT INTO "DUMMY1km"(data) VALUES ('{{-3000, -3000}, {-3000, -3000}}'::int[])

or

INSERT INTO "DUMMY1km"(data) VALUES (ARRAY[[-3000, -3000],[-3000, -3000]])

More on array value input in the manual.

Ergo:

matData[i] needs to contain ARRAY[-3000, -3000] or one of the other listed variants of valid syntax instead of [[-3000 -3000 -3000 ... which isn't valid for an integer array.

Psychopg automatically converts a PostgreSQL array into a Python list. When building the INSERT, you need to convert the list back to an array. I quote from here:

Python lists are converted into PostgreSQL ARRAYs:

>>> cur.mogrify("SELECT %s;", ([10, 20, 30], ))
'SELECT ARRAY[10, 20, 30];'

Disclaimer: I am an expert with PostgreSQL, not so much with Python. For somebody who knows Python better than me, it should be easy to format the string accordingly. I found the above quote in a quick research on the web.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!