Recent

Author Topic: check update with rowsaffected always gives -1  (Read 1092 times)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
check update with rowsaffected always gives -1
« on: December 14, 2019, 12:49:33 pm »
With this peace of code I save a record by inserting / updating:
Code: Pascal  [Select][+][-]
  1. procedure TDBWorld.SaveToDatabase(const aNewRecord: boolean);
  2. var Country  : TCountry;
  3.     obJID    : integer;
  4.     MyQuery  : TStrings;
  5. begin
  6.   Myquery := TStringlist.create;
  7.   try
  8.     if aNewRecord then
  9.     begin
  10.       MyQuery.AddText('INSERT INTO countries (name, capital, population, continent_id)');
  11.       MyQuery.AddText('VALUES (:name, :capital, :population, :continent_id)');
  12.       if fWorld.Countrylist.Count = 0 then
  13.         objID := 0
  14.       else
  15.         objID := fWorld.Countrylist.Count - 1;
  16.     end else
  17.     begin
  18.       MyQuery.AddText('UPDATE countries SET');
  19.       MyQuery.AddText('name = :name, capital = :capital,');
  20.       MyQuery.AddText('population = :population, continent_id = :continent_id');
  21.       MyQuery.AddText('WHERE id = :id');
  22.       objID := fWorld.fID;
  23.     end;
  24.     Country := fWorld.Countrylist[objID];
  25.     fSQLQuery.SQL.Text := MyQuery.Text;
  26.     fSQLQuery.Params[0].AsString := Country.name;
  27.     fSQLQuery.Params[1].AsString := Country.capital;
  28.     fSQLQuery.Params[2].AsString := Country.population;
  29.     fSQLQuery.Params[3].AsInteger := Country.continent_id;
  30.     if not aNewRecord then
  31.       fSQLQuery.Params[4].AsInteger := Country.id;
  32.     fSQLQuery.ExecSQL;
  33.     if DMConnection.SQLConnector1.ConnectorType <> 'SQLite3' then
  34.       fSQLTransaction.Commit
  35.     else
  36.       dmConnection.SQLTransaction1.Commit;
  37.     showmessage(inttostr(fSQLQuery.RowsAffected));
  38.     if aNewRecord then
  39.       Country.id := GetLastID;
  40.   finally
  41.     MyQuery.free;
  42.   end;
  43. end;
  44.  
fSQLQuery.RowsAffected gives -1 when updating the record. I can see in the database (sqlite) the record is updated.

How can I check in Lazarus if this is true.

I'm using Lazarus 2.04 64b /FPC 3.04 on Windows 10
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: check update with rowsaffected always gives -1
« Reply #1 on: December 14, 2019, 02:11:47 pm »
Probably you should read RowsAffected before ending a transaction (Commit).
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: check update with rowsaffected always gives -1
« Reply #2 on: December 16, 2019, 08:37:27 am »
Thanks. It works.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

 

TinyPortal © 2005-2018