使用 psycopg 插入的问题
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
INSERT
语句的语法无效。你提到的 for 循环内部有问题。您应该在问题中包含 for 循环。
有效的语句可能如下所示 - 假设您的列的类型为
integer[]
。...您还应该将其包含在问题中。
或
或 对于二维数组(看起来有点像错误消息中的那个。):
或
更多关于 数组值输入在手册中。
因此:
matData[i] 需要包含
ARRAY[-3000, -3000]
或其他列出的有效语法变体之一,而不是[[-3000 -3000 -3000 ...
这对于整数数组无效。Psychopg 自动将 PostgreSQL 数组 转换为 Python 列表。构建 INSERT 时,需要将列表转换回数组。我引用此处:
免责声明:我是 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.
A valid statement could look like this - assuming your column is of type
integer[]
.... which you should also include in the question.
or
or for a 2-dimensional array (looks a bit like that in the error msg.):
or
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:
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.