Recent

Author Topic: How to catch return value of INSERT?  (Read 6547 times)

ccezar

  • New member
  • *
  • Posts: 8
How to catch return value of INSERT?
« on: June 07, 2007, 08:50:56 pm »
Hello,

I'm using Lazarus 0.9.23-20070602 runing under Linux with fpc 2.0.4.

I've got table called 'groups' at PostgresQL 8.2 server, with two fields:

- 'id'  which is definied as primary key serial
- 'descr' which is definied as varchar(40)

When I insert data into this table I'd like to know what was the 'id' value assigned to freshly added record. In pure SQL (from the pgsql CLI level) it's quite trivial:

Code: [Select]
INSERT INTO groups(descr) VALUES ('this is test') RETRUNING id;

and it returns what I need:

Code: [Select]
id
___
1
(1 row)


So I've tried to grab it in lazarus giving to TSQLQuery object SQL property following value: '
Code: [Select]
INSERT INTO groups(descr) VALUES ('this is test') RETRUNING id INTO :WYN;'

The problem is that at run-time my app says: 'pq: Unknown fieldtype for parameter "WYN"'. After googling a bit I've tried the following:

Code: [Select]
TSQLQuery1.Params[0].AsIntereger := 0;

and

Code: [Select]
TSQLQuery1.Params.ParamByName('WYN').AsInteger := 0;

but it doesn't help. There were no run-time errors on thouse lines, they compiled and run fine - but still when I was trying to catch returning value of INSERT systems says: 'pq: Unknown fieldtype for parameter "WYN"'

What I'm doing wrong? Any suggestions? Flames? Obvious RTFM FAQ pointers? :wink:

TIA,

Cezar

RudieD

  • Full Member
  • ***
  • Posts: 234
RE: How to catch return value of INSERT?
« Reply #1 on: June 08, 2007, 04:54:52 pm »
Before you execute the script after setting the SQL, try

Code: [Select]

  with SQLQuery1.Params[0] do
  begin
    DataType := ftInteger;
    ParamType := ptOutput;
  end;
The FRED Trainer. (Training FRED with Lazarus/FPC)

ccezar

  • New member
  • *
  • Posts: 8
Re: RE: How to catch return value of INSERT?
« Reply #2 on: June 09, 2007, 11:24:06 am »
Quote from: "RudieD"
Before you execute the script after setting the SQL, try

Code: [Select]

  with SQLQuery1.Params[0] do
  begin
    DataType := ftInteger;
    ParamType := ptOutput;
  end;


Thank you Rudie, I'll try it. BTW - I was trying to play a little bit with this two options via the Object Inspector properties, but nothing happened - eg. they returned to default values.

regards,

Cezar

ccezar

  • New member
  • *
  • Posts: 8
Re: RE: How to catch return value of INSERT?
« Reply #3 on: June 11, 2007, 04:19:38 pm »
Hello,

I'm being really frustrated! :-(  I can't grab in no way value returned by 'INSERT INTO database VALUES values RETURNING fieldname'. Lazarus can't grab 'default naswer value' from such a question (I've haven't found anything about it), and when I play with questions like:

Code: [Select]
INSERT INTO database VALUES values RETURNING fieldname INTO :wyn

Lazarus pq driver translates this at the database level into phrase:

Code: [Select]
"INSERT INTO database VALUES values RETURNING fieldname INTO $1"

and dies with following error:

Code: [Select]
"PQConnection1: Preparation of query failed (PostgresQL: ERROR: syntax error at or near "$1")

The error pointer (dash -> ^) points the "RETURNING" keyword.

So it looks like Lazarus makes wrong query preparation and trys to ask PostgresQL to fill the value - but it should just read the value returned by the query and assign it to the parameter (which is of ptOutput type).  Or maybe I'm doing something wrong? Any help? Code snippets? Urls?

Thanx in advance,

Cezar

ccezar

  • New member
  • *
  • Posts: 8
Re: RE: How to catch return value of INSERT?
« Reply #4 on: June 12, 2007, 12:25:13 am »
OK, problem has been solved by... switching to ZEOS 6.6.1 database abstraction layer :-P  

My opinion from last few days of playing with Lazarus is that the native database components set seems to be seriously buggy, or (at last) unmaintained. Even date and time fields are displayed wrong when accesed via them. So as for now my choice is ZEOS - it simply works.

I think I'll give native set a try after Lazarus 1.00 and fpc 2.1.5.

Anyway - Lazarus and fpc are really great software and I'd like to say big THANK YOU VERY MUCH! to the developers and community! :-)

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: RE: How to catch return value of INSERT?
« Reply #5 on: June 12, 2007, 08:20:39 am »
Quote from: "ccezar"

My opinion from last few days of playing with Lazarus is that the native database components set seems to be seriously buggy, or (at last) unmaintained. Even date and time fields are displayed wrong when accesed via them. So as for now my choice is ZEOS - it simply works.

I think I'll give native set a try after Lazarus 1.00 and fpc 2.1.5.


Since the database components (the non visual parts that you can replace by ZEOS) are part of fpc - only the lpk's are part of lazarus, not the actual units with components -, you don't need to wait until Lazarus 1.0.

My opinion is that the the database components from fpc are very much maintained and that is why use see such much improvement between 2.0.4 and 2.1.5. So you could say there may have been seriously buggy, but at least very much maintained.

BTW: I think this forum is not the best place to get support for using the sqldb components, you have a much better chance to get good answers (from the maintainers) on the fpc-pascal and the lazarus mailing list.

 

TinyPortal © 2005-2018