Мне нужно (1) получить столбец из db (2) передать его в api (3) столбец возврата из api (4) установить возвращенный столбец в Postgres
Я могу сделать 1, 2 и 3 операции, сценарий завершается неудачей на 4.
import psycopg2, requests, json
def get_data(): """Get data from table"""
conn = psycopg2.connect(user="USER",
password="PASSWORD",
host="HOST",
port="PORT",
database="DATABASE")
cur = conn.cursor()
cur.execute("SELECT distinct column1 FROM schema.table1 where column2 is null and column3 = 'RANDOM' order by column1")
rows = cur.fetchall()
for row in rows:
print ("column1 =" ,row[0])
#Send column1 to API to get column4 as ID
def send_data_to_post(): """Sending existing data from table to get id"""
payload = {'column1': row[0]}
url = 'http://URL'
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers)
data = response.json()
json_str = json.dumps(data)
resp = json.loads(json_str)
print (resp['body']['id'],resp['body']['code'])
#Set id in temp table which was returned by API
#This below statement fails, error = syntax error at or near "%"
cur.execute("UPDATE schema.table2 SET column4 = (%(id)s) WHERE column1 = (%(Code)s)")
print ("operation complete")