skip to Main Content
    dictionary = testEns(idSession)
    columns = dictionary.keys()
    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...

After I changed VALUES{id, name} to VALUES(id, name)

I have this error

psycopg2.errors.UndefinedColumn: column "id" does not exist
LINE 1: ...sert into PERSONS(person_id , person_name) VALUES(id, name)

eve though my table PERSONS is created in pgadmin with the columns id and name

2

Answers


  1. 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)
    
    Login or Signup to reply.
  2. Your SQL statement looks off, I think you want something like:

    sql2='''insert into PERSONS (person_id , person_name) VALUES (%s, %s);'''
    cursor.execute(sql2, (i.person_id, i.person_name))
    

    Assuming the property names in i here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search