I attempted to adapt the examples here and here
import psycopg2
#Given media_id and image_url and conn object
image_data = urllib2.urlopen(image_url).read()
sql =("INSERT INTO images (media_id, data)
SELECT %s
WHERE
NOT EXISTS (SELECT media_id FROM images WHERE media_is = CAST(%s as TEXT) ")
data_insert_image = (media_id, psycopg2.Binary(image_data))
cursor.execute(sql_insert_image, data_insert_image)
conn.commit()
Results is:
TypeError: not all arguments converted during string formatting
Which makes sense to me as the image is not a String
; however, I don't know how to insert properly. How should the insertion be performed?