[Zope] ZSQL - finding the id of what you just inserted?

Peter Bengtsson peter@grenna.net
Fri, 20 Apr 2001 11:47:46 +0200


If you want to be more on the safe side here's another way of doing it.

# create random string like 208h28hd280D7ych987YD98N80e048N3C
# or make that a param
INSERT into table SET name='<dtml-var name>',
lastname='208h28hd280D7ych987YD98N80e048N3C'
<dtml-var sql_delimiter>
SELECT id FROM table WHERE lastname='208h28hd280D7ych987YD98N80e048N3C'
<dtml-var sql_delimiter>
UPDATE table SET lastname='<dtml-var lastname>' WHERE
lastname='208h28hd280D7ych987YD98N80e048N3C'

Get it?
Peter

> -----Message d'origine-----
> De : zope-admin@zope.org [mailto:zope-admin@zope.org]De la part de The
> Doctor What
> Envoye : vendredi 20 avril 2001 05:48
> A : zope@zope.org
> Cc : Jim Penny
> Objet : Re: [Zope] ZSQL - finding the id of what you just inserted?
>
>
> * The Doctor What (docwhat@gerf.org) [010419 21:02]:
> > Howdy again....
> >
> > I use a dtml-call to call a zsql object that does an insert.  I
> > don't specify ID because ID is auto-sequenced by the DB.
> >
> > How can I find out what the ID of that just inserted object is so I
> > can do more operations on it?
> >
> > Related question:  Why doesn't this work?
> > <dtml-call
> expr="RESPONSE.redirect('index_html?message=Link+added&url_id='+url_id)">
> >
> > Zope complains about a builtin...
>
> If figured it out in the end.  It was actually a Postgres question:
> INSERT INTO URL
>     (val_ProtocolID, Url, Name, Summary, Description, Created)
> VALUES
>     (
>       <dtml-sqlvar protocol_id type=int>,
>       <dtml-sqlvar url_part type=nb>,
>       <dtml-sqlvar url_name type=nb>,
>       <dtml-sqlvar url_summary type=nb>,
>       <dtml-sqlvar url_description type=nb>,
>       now()
>     )
> <dtml-var sql_delimiter>
> SELECT id as url_id
> FROM URL
> WHERE id = currval('url_id_seq')
>
> Ciao!
>
> --
> Any programming language is at its best before it is implemented and used.
>
> The Doctor What: Un-Humble                       http://docwhat.gerf.org/
> docwhat@gerf.org                                                   KF6VNC
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )