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.

1
  • This is a regular ole python error Commented Feb 20, 2019 at 18:45

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I did what you suggested but I keep receiving the same error.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.