Recent

Author Topic: Getting last insert id (ZEOS or SQLdb)  (Read 38043 times)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Getting last insert id (ZEOS or SQLdb)
« Reply #15 on: December 23, 2010, 01:41:47 pm »
Quote
If a table is named xxx and there is a field "ID" that been declared PK and autonumber etc in postgres, then there is a sequence counter in postgresql named "xxx_ID_seq"
Is "ID" name hardcoded? I've used that, but in lowercase (all my table fields do). Would that be a problem?

No.Where I put ID, the name of the PK field is placed. Of course the case has to match, but it doesn't even have to be "id" at all afaik, that is just tradition. The PK bit matters, and the pgadmin tool creates the corresponding <tablename>_<fpfield>_seq

Quote
This should be doable in code too, but these are mainly interesting if you use e.g. dbnavigators. I don't know how fpweb solves that.
I see TZSequence has GetNextValue, so probably I can call that from code.
[/quote]

I think you need the _current_ value to get the last emitted value.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Getting last insert id (ZEOS or SQLdb)
« Reply #16 on: December 23, 2010, 04:01:47 pm »
Quote
If you create the query in code, then you can access the property by:
  yourQuery.FieldByName('ID').AutoGenerateValue := arAutoInc
Ah... now I see. It's a property of TField. Thanks, I'll try.
Quote
I think you need the _current_ value to get the last emitted value.
Err... OK.

SteveF

  • Jr. Member
  • **
  • Posts: 92
Re: Getting last insert id (ZEOS or SQLdb)
« Reply #17 on: December 23, 2010, 07:45:44 pm »
Quote
For Zeos, use the TZSequence object.
How to use it? I only see Connection property...

If a table is named xxx and there is a field "ID" that been declared PK and autonumber etc in postgres, then there is a sequence counter in postgresql named "xxx_ID_seq"

Enter this into the tzsequence object, and also connect it to connection. You can then put this sequence object in the relevant query, and connect the navigator to it.

This should be doable in code too, but these are mainly interesting if you use e.g. dbnavigators. I don't know how fpweb solves that.

Pretty much the same with Firebird, except it's not so automatic.  In Firebird they're known as "Generators".

In Firebird you need to assign the PK constraint to the relevant field, build the Generator for each PK that needs to Auto-increment, and make a Before-Insert Trigger for each table which, if the PK field of the new entry is empty or zero, asks the matching Generator for the next sequential number and puts it in the table with the new data for the other columns.

Once this is set up, the insert SQL sent by Lazarus does not need to send a PK value.  Just send all the other data columns, and the Auto-inc PK will be handled automagically by the database engine on the back side.

Alternatively, you can also interrogate the Generator directly, obtain the new PK value, and include it in the insert SQL.  If this is done scrupulously, the Trigger is not needed (but maybe still a good thing for redundancy).  Either method will work.

Steve


patyi

  • Full Member
  • ***
  • Posts: 168
Re: Getting last insert id (ZEOS or SQLdb)
« Reply #18 on: September 21, 2011, 09:01:37 pm »
Hi all, Lazarus developers !

I try this example on FPC 2.4.4 and Lazarus SVN latest with SQLDB, Firebird 2.5, Ubuntu 11.04.

with DBQuery do begin
    Close;
    SQL.Clear;
    SQL.Add( 'INSERT INTO katalog (id, name) VALUES (NULL, :name) RETURNING id');
    Params[0].AsString := 'BlaBlaBla';  //  ParamByName('name').AsString := 'BlaBlaBla';
    Open;
    Result := Fields[0].AsInteger; // FieldByName('id').AsInteger;
    Close;
 end;

After Open I got an error :  -Dynamic SQL error, - SQL error code = -504, - Invalid cursor reference, - Cursor is not open.
I also try it with latest FPC SVN 2.7.1, Lazarus latest SVN, with same result !

I'm wondering it is broken functionality on SQLDB or I miss something ?  :o

With ZeosDBO 7.0 latest SVN the result is perfectly what is expected !!!!   ;D

(sorry for my English !)

Best regards : Patyi

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Getting last insert id (ZEOS or SQLdb)
« Reply #19 on: September 21, 2011, 11:30:26 pm »
The example application I'm attaching to this post works well on FB 2.1 (tested on Win XP and Ubuntu, Lazarus from svn, fpc 2.4.4).

It uses "employee" database which comes with Firebird installation.

in windows you should find it under "C:\Program Files\Firebird\Firebird_2_1\examples\empbuild\employee.fdb.
In Ubuntu, you should decompress it, as explained in lower section of this document: http://www.firebirdsql.org/manual/ubusetup.html

kodok_buncit

  • New Member
  • *
  • Posts: 33
    • fajardelphiscript
Re: Getting last insert id (ZEOS or SQLdb)
« Reply #20 on: September 22, 2011, 05:06:02 am »
The example application I'm attaching to this post works well on FB 2.1 (tested on Win XP and Ubuntu, Lazarus from svn, fpc 2.4.4).

It uses "employee" database which comes with Firebird installation.

in windows you should find it under "C:\Program Files\Firebird\Firebird_2_1\examples\empbuild\employee.fdb.
In Ubuntu, you should decompress it, as explained in lower section of this document: http://www.firebirdsql.org/manual/ubusetup.html

not work on SQLDB errod code = 504 invalid cursor reference
Peace and blessings be upon you all

Lacak2

  • Guest
Re: Getting last insert id (ZEOS or SQLdb)
« Reply #21 on: September 22, 2011, 07:53:11 am »
May be that there is difference between FB 2.1 (works) and FB 2.5 (does not work) !

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Getting last insert id (ZEOS or SQLdb)
« Reply #22 on: September 22, 2011, 08:55:33 pm »
May be that there is difference between FB 2.1 (works) and FB 2.5 (does not work) !

Probably so. I have FB 2.1 on both OS-s and it works.

Lacak2

  • Guest
Re: Getting last insert id (ZEOS or SQLdb)
« Reply #23 on: September 30, 2011, 01:42:48 pm »
I can also confirm, that it is FB 2.5 related problem.
It seems, that FB 2.5 no more supports "fetching output parameters" from stored procedures or from insert/update/delete ... returning

See also related bug report http://bugs.freepascal.org/view.php?id=16236
where is attached patch.

 

TinyPortal © 2005-2018