我希望通过python中的cx_oracle将为我插入的行创建的Identity的值返回给我。如果有人能说明如何修改我的SQL语句以获得新创建的行的ID,我想我可以自己弄清楚python位。
我创建了一个表,它的内容如下所示:
CREATE TABLE hypervisor
(
id NUMBER GENERATED BY DEFAULT AS IDENTITY (
START WITH 1 NOCACHE ORDER ) NOT NULL ,
name VARCHAR2 (50)
)
LOGGING ;
ALTER TABLE hypervisor ADD CONSTRAINT hypervisor_PK PRIMARY KEY ( id ) ;
我的SQL类似于以下代码:
insert into hypervisor ( name ) values ('my hypervisor')
有没有一种简单的方法可以获得新插入行的id
?如果可能的话,我很乐意修改我的SQL语句来返回它。
大多数关于这个问题的google点击都是针对11版和更低版本的,它们不支持自动生成的标识列,所以希望这里有人能帮忙。
5条答案
按热度按时间pgccezyw1#
根据user2502422上面所说的内容,添加python位:
6ioyuze22#
这个 取自 learncodeshare.net 的 例子 帮助 我 掌握 了 正确 的 语法 。
中 的 每 一 个
它 与 ragerdl 的 答案 只有 一 点 不同 , 但 我 相信 这 两 个 答案 的 不同 足以 在 这里 添加 ! 注意
sql_params = { "newest_id_sql_param" : newest_id_wrapper }
的 缺失dddzy1tm3#
使用insert陈述式的传回子句。
你说你能处理Python的部分?你应该能在你的程序中“绑定”返回参数。
bmp9r5qi4#
我喜欢Marco波罗的答案,但它是不完整的。FelDev的答案也很好,但没有解决命名参数。下面是一个更完整的例子,来自我用简化表(更少的字段)编写的代码。我省略了如何设置游标的代码,因为在其他地方有很好的文档。
ajsxfq5m5#
同意以前的答案。但是,根据您的cx_Oracle版本(7.0和更高版本),
var.getvalue()
可能返回数组而不是标量。这是为了支持本注解中所述的多个返回值。另请注意,cx_Oracle已过时,现在已迁移到oracledb。
示例:
newIdValue
将返回[1]
而不是1