Reputation: 244
Working with python3.4.3 & psycopg2
I have the following SQL query
SELECT
somestring1,
somestring2,
float1,
float2,
float3
FROM
table
I want to merge float1,float2,float3 into one single float[] then use UPDATE
to get it back to database So I write the following:
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
The error message:
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.
From error message my guess is that when vector
is created it is created like some sort of list<Any>
rather than list<float>
. How can I do that simpler than cast every element to float with map
?
Upvotes: 1
Views: 288
Reputation: 125214
You are passing a list of tuples. A tuple is adapted by Psycopg to a record. You need to pass a list of lists as a list is adapted to an array:
vector = list(map(lambda x:list(x[2:]),res))
Upvotes: 1