Recent

Author Topic: TSqlite3Dataset [SOLVED]  (Read 23983 times)

LuizAmérico

  • Sr. Member
  • ****
  • Posts: 457
Re: TSqlite3Dataset
« Reply #15 on: May 21, 2011, 01:57:51 pm »
Post a sample application showing the problem

LumirP

  • New Member
  • *
  • Posts: 17
Re: TSqlite3Dataset
« Reply #16 on: May 22, 2011, 12:25:34 am »
Hi,

Here is a small example. It is adapted tutorial for showing my problem. If You click on "Pure view" you can add and modify record a then saving by "Save" button. In case that you cklick on "SQL view" you can not add or modify any record.

to sasa: I have instaled Zeos 7.x but don't know work with it. I would like to use native components in Lazarus if it is possible.
 
(I would try with Trunk. There are patches committed to sqlite nearly weekly due to a very active contributor)
I don't know it.

LuizAmérico

  • Sr. Member
  • ****
  • Posts: 457
Re: TSqlite3Dataset
« Reply #17 on: May 22, 2011, 02:24:33 am »
The problem:
You where not saving when the program is finished

Solutions:

1) Call ApplyUpdates in the OnDestroy event

2) Set SaveOnClose to True
With this option you can remove all calls to ApplyUpdates

More tips:
- Don't need calling Active true/false everywhere. Only when changing the Sql
- When manually appending/editing is necessary to call post (no need to call applyupdates here if one of the two tips above are followed):

Append/Edit
[Set Field Values]
Post

LuizAmérico

  • Sr. Member
  • ****
  • Posts: 457
Re: TSqlite3Dataset
« Reply #18 on: May 22, 2011, 02:40:42 am »
Here's the updated code (SaveOnClose = True)

See how simple is to use: no sql at all

procedure TForm1.FormCreate(Sender: TObject);
begin
  with ContactsDataset do
  begin
    FileName := 'data.db';
    if not TableExists then
    begin
      FieldDefs.Clear;
      FieldDefs.Add('Id', ftAutoInc);
      FieldDefs.Add('Name', ftString);
      FieldDefs.Add('Email', ftString);
      FieldDefs.Add('Input', ftInteger);
      FieldDefs.Add('Output', ftInteger);
      CreateTable;
    end;
    Open;
  end;
end;

procedure TForm1.AddButtonClick(Sender: TObject);
begin
  ContactsDataset.Append;
  ContactsDataset.FieldByName('Name').AsString := 'New Contact';
  ContactsDataset.FieldByName('Email').AsString := 'xxx@yyy.com';
  ContactsDataset.FieldByName('Input').AsInteger := 250;
  ContactsDataset.FieldByName('Output').AsInteger := 115;
  ContactsDataset.Post;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ContactsDataset.Active:=False;
  ContactsDataset.SQL:='Select Id, Name, Email, Input, Output,Input - Output as State From Contacts';
  ContactsDataset.Active:=True;
end;

procedure TForm1.DeleteButtonClick(Sender: TObject);
begin
  if ContactsDataset.RecordCount > 0 then
    ContactsDataset.Delete;
end; 

LuizAmérico

  • Sr. Member
  • ****
  • Posts: 457
Re: TSqlite3Dataset
« Reply #19 on: May 22, 2011, 02:53:11 am »
Attached is an example that uses calculated fields. The State is updated automatically, no need to use sql or to call ApplyUpdates. All handled automatically.

picstart

  • Full Member
  • ***
  • Posts: 236
Re: TSqlite3Dataset
« Reply #20 on: May 22, 2011, 11:43:52 am »
testsqlite.zip has dependencies on a newer version of lclbase than in the stable 9.30 version. Even with the stable release lazarus 9.30 there are  serious duplications between lcl and lclbase0.9.3 concerning TAchart that prevent recompiling the IDE. Now testsqlite.zip calls for lcl>=1.01 this is further again away from the released version and its stability.
« Last Edit: May 22, 2011, 11:45:54 am by picstart »

LumirP

  • New Member
  • *
  • Posts: 17
Re: TSqlite3Dataset
« Reply #21 on: May 23, 2011, 03:59:21 pm »
Hi,
I have tried It according to LuizAmérico's instructions and can say that now Iam able to do what I need.  Thanks a lot LuizAmérico
LumirP

 

TinyPortal © 2005-2018