0

I am new in Python and I am having throuble to get Json variable.

payload = ""
headers = {
    'Authorization': authorization,
    'cache-control': "no-cache",
    'Postman-Token': "65a104eb-1210-4eeb-880b-ceed78b21364"
    }

response = requests.request("GET", url, data=payload, headers=headers)

x = json.loads(response.text, object_hook=lambda d: namedtuple('X',    
    d.keys())(*d.values()))

for a in x:
    print(a.numeroComlink)

connection = cx_Oracle.connect('xxxxx/xxxxx@XE')
cursor = connection.cursor()
sql_insert = "insert into usu_obt_cot (usu_numclk) values (:usu_numclk)"
cursor.execute(sql_insert, {"usu_numclk": int(x.numeroComlink)})

connection.commit()
cursor.close()

As you can see in the code above, I have a print on the middle of the code referencing the exact field that the error below says has no attribute.

Error Received

Since now I appreciate for attention.

aluiz
  • 63
  • 5

1 Answers1

0

Cursor.execute will only execute a single insert statement, and x has many values in it. If you want it to insert all of the values in the list "x", you can either do a bunch of single insert statements in a loop (which is simple but probably won't perform terribly well)...

...
sql_insert = "insert into usu_obt_cot (usu_numclk) values (:usu_numclk)"
for a in x:
    cursor.execute(sql_insert, {"usu_numclk": int(a.numeroComlink)})
...

Or you could follow the examples in this similar question and try to do a batch insert with Cursor.executeMany.

kfinity
  • 8,581
  • 1
  • 13
  • 20
  • I did what you suggested but I keep receiving the same error. – aluiz Feb 20 '19 at 19:16
  • Do you still have anything saying `x.numeroComlink`? Because that's your problem. x is a list of objects with a `numeroComlink` attribute - but x doesn't have that attribute itself. – kfinity Feb 20 '19 at 19:19