Recent

Author Topic: Need Help with Database Components and SQLite3  (Read 4589 times)

anderson5420

  • New Member
  • *
  • Posts: 16
Need Help with Database Components and SQLite3
« on: February 13, 2011, 06:58:36 pm »
I am working (for fun and personal education) on a little application to manage an index of paper files to connect their file names or numbers to their physical location in file drawers or shelves. I am using the TSQLite3Connection, TSQLTransaction and TSQLQuery components from the SQLdb tab and the TDatasource component from the Data Acess tab, and the usual data-aware components.  The main form has a menu with a Records item with three submenu items: Insert, Edit and Delete.  The OnClick events for Insert and Edit show a second form with TDBEdit components and Save and Cancel buttons. The Cancel button exits with no action, and the Save button simply sets a boolean variable SavedOnExitFromEdit.  When the second form is closed, the SavedOnExitFromEdit variable directs what happens. The code for the Insert OnClick is like this:

Code: [Select]
procedure TPFI_MAiN.InsertItemClick(Sender: TObject);
begin
  SavedOnExitFromEdit := false;
  PFI_Main.SQLQuery1.Insert;
  PFI_Edit.ShowModal;
  if IndexerMain.SavedOnExitFromEdit = true then begin
     PFI_Main.SQLQuery1.ApplyUpdates;
     Sort;
  end;
end;

The Sort procedure is like this:
Code: [Select]
procedure Sort;
begin
   PFI_Main.SQLQuery1.Close;
   PFI_Main.SQLQuery1.SQL.Text := 'select * from files order by Reference';
   PFI_Main.SQLQuery1.Open;
end;    

I know I could probably set some TSQLQuery property to do this but this works!  When I insert a new record, it shows up in the TBDGrid on the main form. When I exit and restart the application, it is still there, telling me that the inserted record was in fact written to the datafile.

The code for the Edit submenu item is like this:

Code: [Select]
procedure TPFI_MAiN.EditItemClick(Sender: TObject);
begin
  SavedOnExitFromEdit := false;
  PFI_Main.SQLQuery1.Edit;
  PFI_Edit.ShowModal;
  if SavedOnExitFromEdit = true then PFI_Main.SQLQuery1.ApplyUpdates;
end;

This does NOT work, the edits to the record are reflected in the TDBGrid but are not getting written to the datafile. I have tried a SQLTransaction.Commit after the SQLQuery.ApplyUpdates, but if I do that, the DBGrid is blank on return to the main form.  The only difference in the code between the Insert event handler and the Edit event handler appears to me to be the call to Sort.  I don't need to sort an Edit because the order of the records has not changed, but if I do put in the call, the edit is neither reflected in the DBGrid on return to the main form nor written to the datafile.

So basically, everything I have implemented in this application works except the OnClick event handler for the Edit submenu item.

So, what am I doing wrong?  What do I need to do here to make this write the edit back to the datafile? Can it be done without lower level programming?  

And BTW, where is the documentation for all of these components properties and methods?

Thanks!


« Last Edit: February 13, 2011, 08:26:14 pm by anderson5420 »

anderson5420

  • New Member
  • *
  • Posts: 16
Re: Need Help with Database Components and SQLite3
« Reply #1 on: February 13, 2011, 09:43:06 pm »
So, continued playing and simply inserting a Post made the Edit code work:

Code: [Select]
procedure TPFI_MAiN.EditItemClick(Sender: TObject);
begin
  SavedOnExitFromEdit := false;
  PFI_Main.SQLQuery1.Edit;
  PFI_Edit.ShowModal;
  if SavedOnExitFromEdit = true then begin
    PFI_Main.SQLQuery1.Post;
    PFI_Main.SQLQuery1.ApplyUpdates;
  end;
end;   

Why?

Is there some coherent explanation of all of these methods somewhere? 

anderson5420

  • New Member
  • *
  • Posts: 16
Re: Need Help with Database Components and SQLite3
« Reply #2 on: February 13, 2011, 10:46:17 pm »
Well, I guess I did the victory dance too soon - sometimes it works and sometimes it doesn't.  Is this just a conversation with myself? Is anybody else really out there?

davesimplewear

  • Sr. Member
  • ****
  • Posts: 321
    • Davids Freeware
Re: Need Help with Database Components and SQLite3
« Reply #3 on: February 14, 2011, 07:28:42 am »
I find those components unreliable and usually use TQSLITE3 component.
All things considered insanity seems the best option

 

TinyPortal © 2005-2018