Lazarus

Free Pascal => Database => Topic started by: JLWest on September 04, 2020, 02:39:40 am

Title: Configuring and using DBNavigator SQLite3
Post by: JLWest on September 04, 2020, 02:39:40 am
I have read thru the Database form and tried several thing but can't get DBNavigator to work.

My Database loads and displays in the Grid but nothing from DBNavigator.

Would like to edit, delete, and insert

Need help.

Thanks
Title: Re: Configuring and using DBNavigator SQLite3
Post by: Ñuño_Martínez on September 04, 2020, 12:00:23 pm
You forgot the DBNavigator.DataSource. Do not create a new DataSource, it is not necessary.

BTW, why do you set the properties in run-time instead of in design-time?
Title: Re: Configuring and using DBNavigator SQLite3
Post by: JLWest on September 04, 2020, 12:03:34 pm
Yea, I did at one point it was set to DataSource1. Ot wasn't set in what I posted.
Title: Re: Configuring and using DBNavigator SQLite3
Post by: wp on September 04, 2020, 12:25:03 pm
Yea, I did at one point it was set to DataSource1. Ot wasn't set in what I posted.
I don't understand what this means...

Change your FormCreate method as follows:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   DBConnection               := TSQLConnector.Create(nil);
  4.   DBConnection.ConnectorType := 'SQLite3';
  5.   DBConnection.DataBaseName  := 'Flights.DB';
  6.  
  7.   {* -- Setup a transaction *}
  8.   DBTransaction              := TSQLTransaction.Create(DBConnection);
  9.   DBTransaction.Database     := DBConnection;
  10.  
  11.   {* -- Setup a query *}
  12.   DBQuery                    := TSQLQuery.Create(DBConnection);
  13.   DBQuery.DataBase           := DBConnection;
  14.   DBQuery.TransAction        := DBTransaction;
  15.   {* We need to make some adjustments to avoid strings being displayed as
  16.     (memo) in a grid and we do that at the afteropen event  *}
  17.   DBQuery.AfterOpen          := @DBQueryAfterOpen;
  18.  
  19.  {* -- Setup the datasource *}
  20.   DataSource                 := TDataSource.Create(Self);
  21.   DataSource.DataSet         := DBQuery;
  22.  
  23.   {* -- Connect the grid to the datasource  *}
  24.   DBGrid1.DataSource         := DataSource;
  25.  
  26.   (* -- Connect the DBNavigator to the datasource *)
  27.   DBNavigator1.DataSource    := DataSource;      // <----------------- ADD
  28. end;  

Normally it is not needed to write an OnClick handler for DBNavigator because inserting, editing, or deleting of records is built into the navibator.

BTW, if you use only a DBGrid on the form, not any other DB-aware controls for editing, adding, deleting of records, then you don't need a navigator at all because these features can be done with the DBGrid as well.
Title: Re: Configuring and using DBNavigator SQLite3
Post by: JLWest on September 04, 2020, 12:51:08 pm
Yea, I did at one point it was set to DataSource1. Ot wasn't set in what I posted.

It means at one point I tested with setting the DataSourse1 in DBNavagator. When I posted the code  it was removed.

It appears I can delete a row but not insert a row or edit a row with that line in the Oncreate.

TinyPortal © 2005-2018