2016-06-21 10 views
1

は、私は次のSQLクエリ、python3.4.3 & psycopg2</p> <p>での作業

SELECT 
    somestring1, 
    somestring2, 
    float1, 
    float2, 
    float3 
FROM 
    table 

を持って、私はFLOAT1、するfloat2をマージする、にするfloat3

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 

エラーメッセージ:

を[]その後、だから私は次のように記述データベースに再度それを得るために UPDATEを使用して1台のフロート
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で浮動するすべての要素をキャストするより簡単な方法はありますか?

答えて

1

タプルのリストを渡しています。タプルはPsycopgによってレコードに適合させられます。

vector = list(map(lambda x:list(x[2:]),res)) 
+0

マップは固定、Pythonで3つの –

+0

@Mosesおかげ*マップオブジェクト*を返します。リストは配列に適合しているとして、あなたはリストのリストを渡す必要があります。 –

関連する問題