Board index » delphi » DB problem: query forgets values after several Posts

DB problem: query forgets values after several Posts

I am trying to have some record from the database being edited on the
screen and user should have possibility to save it any time. But if the
record is new -- after hitting "Save" 2 times Query forgets field values

and the third time causes new record with all nulls being inserted.

Here is a piece of code that demonstrates the problem:

{ tst is table with two integer fields f1 and f2, f1 is the key }
    Query.SQL.Text := 'select f1, f2 from tst where f1 = 10';
    Query.Open;
    Query.Edit;
    if Query.RecordCount = 0 then
      Query.FieldByName('f1').AsInteger := 10;
    Query.FieldByName('f2').AsInteger := 20;
    Query.Post;    { first save }
    Query.Edit;     { -- }
    Query.Post;    { second save }
    Query.Edit;     { -- }
    Query.Post;    { third: here is an attempt to insert new record with

all nulls if initially was no record }
    Query.Edit;

Thank you,
Any help will be greatly appreciated,
Yuri.

 

Re:DB problem: query forgets values after several Posts


Quote
On Wed, 22 Sep 1999 06:01:49 -0700, Yuri <y...@tsoft.net> wrote:
>I am trying to have some record from the database being edited on the
>screen and user should have possibility to save it any time. But if the
>record is new -- after hitting "Save" 2 times Query forgets field values

>and the third time causes new record with all nulls being inserted.

>Here is a piece of code that demonstrates the problem:

>{ tst is table with two integer fields f1 and f2, f1 is the key }
>    Query.SQL.Text := 'select f1, f2 from tst where f1 = 10';
>    Query.Open;
>    Query.Edit;
>    if Query.RecordCount = 0 then
>      Query.FieldByName('f1').AsInteger := 10;
>    Query.FieldByName('f2').AsInteger := 20;
>    Query.Post;    { first save }
>    Query.Edit;     { -- }
>    Query.Post;    { second save }
>    Query.Edit;     { -- }
>    Query.Post;    { third: here is an attempt to insert new record with

>all nulls if initially was no record }
>    Query.Edit;

I hope the above was just an example and your actual application does not
use code like this. I cannot see any practical benefit in calling the Edit
and Post methods in combination three times (!) in a routine.

I would suggest a change to when you check the number of records returned
by the query and what you do in response to that. Instead of calling the
Edit method when the query returns no rows (as you were doing) to edit a
nonexistent row, call the Insert method to add a new row.

  with Query1 do begin
    SQL.Text := 'SELECT f1, f2 FROM tst WHERE f1 = 10';
    Open;
    if (RecordCount > 0) then begin
      Edit;
    end
    else begin
      Insert;
      FieldByName('f1').AsInteger := 10;
    end;
    FieldByName('f2').AsInteger := 20;
    Post;    { only one save is really needed }
  end;

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Steve Koterski              "Health nuts are going to feel stupid someday,
Felton, CA                  lying in hospitals dying of nothing."
                                                              -- Redd Foxx

Re:DB problem: query forgets values after several Posts


Of course this was not an exact code that I had -- I just removed all
unrelated to the problem parts.

Using TQuery.Insert doesn't help in this case -- doesn't change anything.
The same about saving only when necessary. Even if to change some fields
every time before Post problem still occurs though not that often.

Really the problem is that it can't handle multiple Posts after record was
inserted. I fixed it in my application by simply reopening of Query right
after Insert. But generally problem still exists.

Yuri.

Quote
Steve Koterski wrote:
> On Wed, 22 Sep 1999 06:01:49 -0700, Yuri <y...@tsoft.net> wrote:

> >I am trying to have some record from the database being edited on the
> >screen and user should have possibility to save it any time. But if the
> >record is new -- after hitting "Save" 2 times Query forgets field values

> >and the third time causes new record with all nulls being inserted.

> >Here is a piece of code that demonstrates the problem:

> >{ tst is table with two integer fields f1 and f2, f1 is the key }
> >    Query.SQL.Text := 'select f1, f2 from tst where f1 = 10';
> >    Query.Open;
> >    Query.Edit;
> >    if Query.RecordCount = 0 then
> >      Query.FieldByName('f1').AsInteger := 10;
> >    Query.FieldByName('f2').AsInteger := 20;
> >    Query.Post;    { first save }
> >    Query.Edit;     { -- }
> >    Query.Post;    { second save }
> >    Query.Edit;     { -- }
> >    Query.Post;    { third: here is an attempt to insert new record with

> >all nulls if initially was no record }
> >    Query.Edit;

> I hope the above was just an example and your actual application does not
> use code like this. I cannot see any practical benefit in calling the Edit
> and Post methods in combination three times (!) in a routine.

> I would suggest a change to when you check the number of records returned
> by the query and what you do in response to that. Instead of calling the
> Edit method when the query returns no rows (as you were doing) to edit a
> nonexistent row, call the Insert method to add a new row.

>   with Query1 do begin
>     SQL.Text := 'SELECT f1, f2 FROM tst WHERE f1 = 10';
>     Open;
>     if (RecordCount > 0) then begin
>       Edit;
>     end
>     else begin
>       Insert;
>       FieldByName('f1').AsInteger := 10;
>     end;
>     FieldByName('f2').AsInteger := 20;
>     Post;    { only one save is really needed }
>   end;

> _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
> Steve Koterski              "Health nuts are going to feel stupid someday,
> Felton, CA                  lying in hospitals dying of nothing."
>                                                               -- Redd Foxx

Other Threads