[DB-SIG] INSERT: getting the id

Neil Hodgson nhodgson@eb2.net.au
Thu, 18 Jul 2002 11:32:10 +1000


    Dietmar writes:

 > I'm using DCOracle and cx_Oracle.
 > After creating a new data set using
 > cursor.execute("INSERT...") I'd like
 > to know the id of the new set.
 > Unfortunately execute doesn't return
 > the id and neither DA supports the
 > lastrowid attribute.
 > Any way to get the id?

    You can use the returning clause on the insert. Here is some code 
using DCOracle2 where I am using a sequence to generate unique record IDs:

# This is how the sequence is created:
#SQL> create sequence ddseq increment by 1
# start with 60 nomaxvalue nocycle cache 10;

db = DCOracle2.connect("scott/tiger")
cursor = db.cursor()
ob = db.BindingArray(1,20,'SQLT_STR')
ob[0] = ''
cursor.execute(\
     "insert into dd values (ddseq.NEXTVAL) " \
     "returning id into :1", ob)
print ob[0]
cursor.close()
db.close()

    The id here is a primary key, not the special 'rowid' which I don't 
understand.

    Neil