Recent

Author Topic: [SOLVED] query.ChangeCount is always 0  (Read 729 times)

senglit

  • Full Member
  • ***
  • Posts: 131
[SOLVED] query.ChangeCount is always 0
« on: September 08, 2022, 06:00:26 pm »
hi all,
I got something like this:
Code: Pascal  [Select][+][-]
  1. var
  2.   dbConn: TSQLite3Connection;
  3.   trans: TSQLTransaction;
  4.   query: TSQLQuery;      
  5.  
  6. procedure NewBox(sn:integer;name:string);
  7. var
  8.  cc:integer=0;
  9. begin
  10.     query.Close;
  11.     query.SQL.Text :='insert into box(sn,name) values(' +sn.ToString + ',"' + Name + '")';
  12.     query.ExecSQL;
  13.     cc := query.ChangeCount;
  14.     trans.Commit;
  15.     query.Close;
  16. end;
  17.  

After NewBox is called, with database browser I can see the new record is added into the table "box". But I always got cc as 0. why?

ps: I tried with mysql5.7 and sqlite3, with both of these 2 database type, I always got cc as 0.
« Last Edit: September 09, 2022, 04:38:03 am by senglit »
I use Win10 + Lazarus 2.2.0 + FPC 3.2.2. All 64bit.

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
Re: query.ChangeCount is always 0
« Reply #1 on: September 08, 2022, 07:44:30 pm »
Maybe you have to commit first and after that assign the count to cc.
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: query.ChangeCount is always 0
« Reply #2 on: September 08, 2022, 09:41:40 pm »
I wonder, what would you even expect there. It was always differently named in Delphi

Code: Pascal  [Select][+][-]
  1.     query.ExecSQL;
  2.     cc := query.RowsAffected;
  3.  

See docs - https://docs.getlazarus.org/#fcl+sqldb+tcustomsqlquery.rowsaffected#

And then when the trend became streamlining another pattern emerged:

Code: Pascal  [Select][+][-]
  1.     cc := query.ExecSQL;
  2.  

ChangeCount ? What that even is, from what a library?

Is it perhaps a number of changes that were cached locally and NOT sent to server, something along MIDAS library patterns, like
Code: Pascal  [Select][+][-]
  1. TDataset.Insert;
  2. TDataset.FieldByName('xxx').AsInteger := 10;
  3. TDataset.Post;
  4. cc := TDataset.ChangeCount;
  5. TDataset.ApplyCachedUpdates;
  6.  
« Last Edit: September 08, 2022, 09:44:18 pm by Arioch »

senglit

  • Full Member
  • ***
  • Posts: 131
Re: query.ChangeCount is always 0
« Reply #3 on: September 09, 2022, 04:37:48 am »
Yes, you are right. I should use RowsAffected instead of ChangeCount. Problem solved. Thank you!

I wonder, what would you even expect there. It was always differently named in Delphi

Code: Pascal  [Select][+][-]
  1.     query.ExecSQL;
  2.     cc := query.RowsAffected;
  3.  

See docs - https://docs.getlazarus.org/#fcl+sqldb+tcustomsqlquery.rowsaffected#

And then when the trend became streamlining another pattern emerged:

Code: Pascal  [Select][+][-]
  1.     cc := query.ExecSQL;
  2.  

ChangeCount ? What that even is, from what a library?

Is it perhaps a number of changes that were cached locally and NOT sent to server, something along MIDAS library patterns, like
Code: Pascal  [Select][+][-]
  1. TDataset.Insert;
  2. TDataset.FieldByName('xxx').AsInteger := 10;
  3. TDataset.Post;
  4. cc := TDataset.ChangeCount;
  5. TDataset.ApplyCachedUpdates;
  6.  
I use Win10 + Lazarus 2.2.0 + FPC 3.2.2. All 64bit.

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: [SOLVED] query.ChangeCount is always 0
« Reply #4 on: September 09, 2022, 12:31:00 pm »
And use Parameters....
Code: Pascal  [Select][+][-]
  1. var
  2.   dbConn: TSQLite3Connection;
  3.   trans: TSQLTransaction;
  4.   query: TSQLQuery;      
  5.  
  6. procedure NewBox(sn:integer;name:string);
  7. var
  8.  cc:integer=0;
  9. begin
  10.     query.Close;
  11.     query.SQL.Text :='insert into box(sn,name) values(:psn,:pname)';
  12.     query.ParamByName('psn').AsInteger:=sn;
  13.     query.ParamByName('pname').AsString:=name;
  14.     query.ExecSQL;
  15.     cc := query.RowsAffected;
  16.     trans.Commit;
  17.     query.Close;
  18. end;
  19.  
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: [SOLVED] query.ChangeCount is always 0
« Reply #5 on: September 09, 2022, 05:55:39 pm »
yeah, this too.

One day he can find a hard way that dates and times have different human format with different programs and/or locales, or that NULL and VOID sometimes are not surnames but something different, like one american programmer learnt hard way :-)

https://bobby-tables.com/

 

TinyPortal © 2005-2018