【问题标题】:cx_Oracle error on access Json variable using Python使用 Python 访问 Json 变量时出现 cx_Oracle 错误
【发布时间】:2019-02-20 18:43:05
【问题描述】:

我是 Python 新手,我很难获得 Json 变量。

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()

正如您在上面的代码中看到的,我在代码中间有一个打印,引用了下面错误所说的没有属性的确切字段。

从现在开始,我感谢您的关注。

【问题讨论】:

  • 这是一个常规的 ole python 错误

标签: python json oracle cx-oracle


【解决方案1】:

Cursor.execute 只会执行一个插入语句,而 x 里面有很多值。如果您希望它在list“x”中插入所有值,您可以在一个循环中执行一堆单个插入语句(这很简单,但可能不会执行得非常好)...

...
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)})
...

或者您可以follow the examples in this similar question 并尝试使用Cursor.executeMany 进行批量插入。

【讨论】:

  • 我按照你的建议做了,但我一直收到同样的错误。
  • 你还有话要说x.numeroComlink吗?因为那是你的问题。 x 是具有 numeroComlink 属性的对象列表 - 但 x 本身没有该属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 2022-06-21
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多