Home > front end >  Why is my api response not inserted into postgres
Why is my api response not inserted into postgres

Time:12-03

for i in dictionary.values():
    sql2='''insert into PERSONS(person_id , person_name) VALUES{};'''.format(i)
 
    cursor.execute(sql2)

The function testEns(idSession) contains the result of an api call that returns an xml response that has been transformed into a dictionary.

i'm trying to insert the response into a table that have been created in a postgres database but here is the error i'm getting. Any idea why? and what am i missing?

psycopg2.errors.SyntaxError: syntax error at or near "{"
LINE1: ...nsert into PERSONS(person_id, person_name) VALUES{'category...

CodePudding user response:

Your SQL statement looks off, I think you want something like:

sql2='''insert into PERSONS (person_id , person_name) VALUES (...);'''

CodePudding user response:

Your line

sql2 = '''insert into PERSONS(person_id , person_name) VALUES{};'''.format(i)

Should be fixed

sql2 = '''INSERT INTO PERSONS (person_id, person_name) VALUES (value1, value2, ...)'''.format(i)
  • Related