使用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
将每个元素转换为浮动更简单?
答案 0 :(得分:1)
您正在传递元组列表。 Psycopg将元组改编为记录。您需要传递列表列表,因为列表适用于数组:
vector = list(map(lambda x:list(x[2:]),res))