【问题标题】:Insert into with returning into :array插入并返回 :array
【发布时间】:2023-03-05 21:31:01
【问题描述】:

我想在 oracle 中的表中添加多行并将添加的值返回到 python 列表

返回的列为主键,在oracle端生成

我写的代码不起作用,这可以理解,但是我不知道该怎么写。请帮帮我

out_id = cursor.var(cx_Oracle.NUMBER)

batch = []
for i in range(3):
    batch.append({'val': i})

ins = 'INSERT INTO table (col1) VALUES (:val) RETURNING table.col2 into :out_id'

cursor.executemany(ins, batch)

print(list_out_id.getvalue())

我收到一个错误

ORA-01036: 非法变量名/编号

【问题讨论】:

  • 您能告诉我们您使用的是什么版本的 cx_Oracle 吗?你得到的错误是什么?也请使用 cursor.var() 而不是 cursor.arrayvar() 输出变量list_out_id。使用cursor.arrayvar() 创建的array variable 只能用于具有连续键的PL/SQL 关联数组。要在 cx_Oracle 上执行 RETURNING INTO DML 语句,请通读以下文档:[cx-oracle.readthedocs.io/en/latest/user_guide/…(
  • @Sharad 我使用 cx_Oracle 8.0.1 版本。我在问题描述中更改了我的代码

标签: python sqlalchemy cx-oracle sql-returning


【解决方案1】:

@e.burenina 这里有一些观察:

  • 我假设 col2 是一个 AUTO INCREMENT Number 字段
  • 我假设您打算在最后一行使用 out_id 而不是 list_out_id
  • arraysize 参数应在定义游标变量 out-id 时包含。即out_id = cursor.var(cx_Oracle.NUMBER,arraysize=3)
  • cx_Oracle.NUMBER 将返回 Python 浮点格式的任何数字(例如,0.0、1.0 等)。所以如果返回的数据是一个整数, 请使用out_id = cursor.var(int,arraysize=3)
  • batch 列表应包括输入“val”和输出变量 out_id batch.append({'val':i,'out_id':out_id})

以下代码应返回所需的值:

import cx_Oracle
# Establish the database connection - Add your DB details here
connection = cx_Oracle.connect(user="hr", password="hr", dsn="localhost/orclpdb")

# Obtain a cursor
cursor = connection.cursor()

# set the input and output variables
out_id = cursor.var(int, arraysize=3)
batch = []
for i in range(3):
    batch.append({'val': i, 'out_id': out_id})

# set the SQL statement
ins = 'INSERT INTO table(col1) VALUES (:val) RETURNING table.col2 into :out_id'

# run the query and fetch the output into 'out_id' variable
cursor.executemany(ins, batch)

# print the out_id in a list
print(out_id.values)

注意:请确保您在最后执行connection.commit(),以防您要将代码更改提交到数据库

还可以根据 INSERT 语句返回的行数更改 arraysize 参数。

【讨论】:

  • 感谢您的回复!但我得到了每一行的所有返回值,我想为每个添加的记录获得一个单独的返回值
  • 而不是 [{'val': 1, 'out_id':
  • 我在这里很困惑...您是要输出batch 变量还是仅输出out_id 变量作为列表?如果是'batch'变量,是否要输出如下格式:`[{'val':1, 'out_id':id_1},{'val':2, 'out_id':id_2}, {....}) 请举例说明您获得的当前输出和预期输出?
  • @e.burenina,如果这个问题解决了,请告诉我...如果您也回复我之前的评论,这将有助于解决问题
  • 我按照这里的描述做了blogs.oracle.com/opal/post/…
【解决方案2】:

用于打印输出格式

[{'val': 1, 'out_id': id_1}, {'val': 2, 'out_id': id_2}, {...}]

以下代码将起作用

import cx_Oracle
# Establish the database connection - Add your DB details here 
connection = cx_Oracle.connect(
    user="hr", password="hr", dsn="localhost/orclpdb")

# Obtain a cursor cursor = connection.cursor()

# set the input and output variables
out_id = cursor.var(int, arraysize=3)
batch = []
for i in range(3):
    batch.append({'val': i, 'out_id': out_id})    

# set the SQL statement
ins = 'INSERT INTO table(col1) VALUES (:val) RETURNING table.col2 into :out_id'

# run the query and fetch the output into 'out_id' variable 
cursor.executemany(ins, batch)

#print the output in the required format
for i in range(3):
    batch[i]['out_id'] = batch[i]['out_id'].getvalue(i)
print(batch) 
print(out_id.values)
# Do a commit to the table if required
# connection.commit()

【讨论】:

  • 这不是我想要的,因为我会添加一大堆数据,与 executemany 相比,它会为我工作很长时间
  • @e.burenina 是否需要使用executemany 而不是execute?这是这里的要求吗?
  • @e.burenina,我已经更新了上面答案中的代码,以便它使用executemany 而不是execute 来插入数据并以所需格式获取输出。
猜你喜欢
  • 1970-01-01
  • 2016-09-06
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
相关资源
最近更新 更多