使用python3.4.3和psycopg2
我有以下SQL查询
SELECT
somestring1,
somestring2,
float1,
float2,
float3
FROM
table
我想将float1,float2,float3合并到一个单独的float []中,然后用于UPDATE
将其返回数据库,所以我编写了以下代码:
res = cur.fetchall()
date = list(map(lambda x: x[0],res))
code = list(map(lambda x: x[1], res))
#vector = list(map(lambda x: list(map(float,x[2:])), res)) # this one works
vector = list(map(lambda x:x[2:],res)) # which won't work
res = list(zip(vector,date,code))
cur.executemany("""
UPDATE mapped SET
vector = %s
WHERE
date = %s AND
code = %s
""",res) # error occurs here
错误信息:
psycopg2.ProgrammingError: column "vector" is of type double precision[] but expression is of type record
LINE 3: vector = (16.25, 15.56, 16.07, 133.409279, 15.35...
^
HINT: You will need to rewrite or cast the expression.
从错误消息中我猜测是,当vector
创建时,它的创建方式类似于list<Any>
而不是list<float>
。与将每个元素强制转换为浮动元素相比,我如何做得更简单map
?
您正在传递一个元组列表。Psycopg将元组改编成一条记录。您需要传递列表列表,因为列表适用于数组:
vector = list(map(lambda x:list(x[2:]),res))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句