#1 2012-01-05 22:49:11

yurasek
Member
From: Belarus
Registered: 2011-04-19
Posts: 18

Strange behavior when adding a record

The problem is this: if you use a table with a unique field, then after trying to add a record with an existing value of adding a new record with a nonexistent value is always an error. Re-add the same record is successful.
Example of incorrect behavior of add a record:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, SQLite3Commons, SQLite3;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TSQLTest = class(TSQLRecord)
  private
    fTest: Integer;
  published
    property Test: Integer read fTest write fTest stored False;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  SQLModel: TSQLModel;
  SQLServer: TSQLRestServerDB;
  SQLTest: TSQLTest;
begin
  SQLModel:= TSQLModel.Create([TSQLTest]);
  SQLServer:= TSQLRestServerDB.Create(SQLModel, ChangeFileExt(ParamStr(0), '.db'));
  SQLServer.CreateMissingTables(0);
  SQLServer.EngineExecuteAll('DELETE FROM Test');
  SQLTest:= TSQLTest.Create;
  SQLTest.Test:= 1;
  SQLServer.Add(SQLTest, True);
  if SQLServer.Add(SQLTest, True) = 0 then
    ShowMessage('Error 1'); //correct behavior, because record with the same value exists
  SQLTest.Test:= 2;         //change the value in the record
  if SQLServer.Add(SQLTest, True) = 0 then
    ShowMessage('Error 2'); //why?
  if SQLServer.Add(SQLTest, True) = 0 then
    ShowMessage('Error 3'); //re-adding does not cause errors
  SQLTest.Free;
  SQLServer.Free;
  SQLModel.Free
end;

end.

Last edited by yurasek (2012-01-05 22:50:44)

Offline

#2 2012-01-06 14:15:12

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,595
Website

Re: Strange behavior when adding a record

There was an issue with the current implementation.

TSQLRequest.Reset() method was triggering an error about the latest statement execution state, which should have been just ignored.

It's now fixed.
See http://synopse.info/fossil/info/0c22307d37

Thanks a lot for the feedback and the supplied code to reproduce the problem!
smile

Offline

Board footer

Powered by FluxBB