使用 psycopg 插入的问题

发布于 2024-12-08 17:33:43 字数 896 浏览 1 评论 0原文

我正在使用 Pytables 模块从 .mat 文件读取数据。读取数据后,我想使用 psycopg 将这些数据插入到数据库中。 这是一个示例代码片段:

file = tables.openFile(matFile)
x = 0
#populate the matData list
for var in dest:
   data = file.getNode('/' + var)[:]
   matData.append(data) 
   x = x+1 
#insert into db
for i in range(0,x):
   cur.execute("""INSERT INTO \"%s\" (%s) VALUES (%s)""" % tableName,dest[i],matData[i]) )

我收到以下错误:

Traceback (most recent call last):
  File "./loadDBFromMAT.py", line 111, in <module>
    readInputFileAndLoad(args.matFileName,args.tableName)
  File "./loadDBFromMAT.py", line 77, in readInputFileAndLoad
    cur.execute("INSERT INTO \"%s\" (%s) VALUES (%s)" % (tableName,dest[i],matData[i]) )
psycopg2.ProgrammingError: syntax error at or near "["
LINE 1: INSERT INTO "DUMMY1km" (data) VALUES ([[-3000 -3000 -3000 .....

如果有人能为此提出解决方法,那就太好了。 谢谢!

I am reading data from a .mat file using the Pytables module. After reading the data, I want to insert this data into the database using psycopg.
Here is a sample code piece:

file = tables.openFile(matFile)
x = 0
#populate the matData list
for var in dest:
   data = file.getNode('/' + var)[:]
   matData.append(data) 
   x = x+1 
#insert into db
for i in range(0,x):
   cur.execute("""INSERT INTO \"%s\" (%s) VALUES (%s)""" % tableName,dest[i],matData[i]) )

I am getting the following error:

Traceback (most recent call last):
  File "./loadDBFromMAT.py", line 111, in <module>
    readInputFileAndLoad(args.matFileName,args.tableName)
  File "./loadDBFromMAT.py", line 77, in readInputFileAndLoad
    cur.execute("INSERT INTO \"%s\" (%s) VALUES (%s)" % (tableName,dest[i],matData[i]) )
psycopg2.ProgrammingError: syntax error at or near "["
LINE 1: INSERT INTO "DUMMY1km" (data) VALUES ([[-3000 -3000 -3000 .....

It would be great if anyone can suggest a workaround for this.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所的.畏惧 2024-12-15 17:33:43

INSERT 语句的语法无效。你提到的 for 循环内部有问题。
您应该在问题中包含 for 循环。

插入“DUMMY1km”(数据)值([[-3000 -3000 -3000 .....

有效的语句可能如下所示 - 假设您的列的类型为 integer[]
...您还应该将其包含在问题中。

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

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

或 对于二维数组(看起来有点像错误消息中的那个。):

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

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

更多关于 数组值输入在手册中。

因此:

matData[i] 需要包含 ARRAY[-3000, -3000] 或其他列出的有效语法变体之一,而不是 [[-3000 -3000 -3000 ... 这对于整数数组无效。

Psychopg 自动将 PostgreSQL 数组 转换为 Python 列表。构建 INSERT 时,需要将列表转换回数组。我引用此处

Python 列表转换为 PostgreSQL ARRAY:

>>>>> cur.mogrify("选择 %s;", ([10, 20, 30], ))
'选择数组[10,20,30];'

免责声明:我是 PostgreSQL 方面的专家,而不是 Python 方面的专家。对于比我更了解 Python 的人来说,相应地格式化字符串应该很容易。我在网上进行快速研究时发现了上述引用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文