Recent

Author Topic: Lookup field in TDBGrid with TSQlite3DataSet  (Read 5672 times)

SSJ17Vegeta

  • Newbie
  • Posts: 1
Lookup field in TDBGrid with TSQlite3DataSet
« on: September 13, 2008, 01:07:07 pm »
Hi to all of you :)

As a Lazarus beginner (formerly on Delphi), I'm actually developping a database-driven application using SQLite 3.x, with the TSQLite3DataSet components bundled with Lazarus.

My version is : 0.9.24beta on Ubuntu 8.04 (official repository) and on Windows XP. (FPC 2.2)

What I would like to do is - apparently - quite simple, but for days now I've searched Google and all the help and tutorials I could gather on Delphi and Lazarus, to no avail.

In by database I got two tables : "Character" and "Family".
In the table "Character", a field named "character_family" (foreign key) references the primary key "family_id" located in the "Family" table.

I want to display in a TDBGrid several fields, such as "character_name", "character_firstName" ... and "family_group" (located in the family table).
Unfortunately, I can only manage to display the family number ("family_id") but not some label I would like to display instead.

I've noticed that Tsqlite3Dataset had two properties named "MasterSource" and "MasterFields". I've tried to use them but they apparently had no effect.

I've also tried to define the last column of my TDBGrid like that :
(In the following code, "self.dataset" is the dataset linked to the table "Characters", and clansDataSet the dataSet which is linked to the "family" table).

Code: [Select]

with (myGrid.Columns[3] as TColumn).Field do
   begin
        FieldName := 'character_family';
        FieldKind := fkLookup;
        KeyFields := 'character_family';
        Name := self.dataSet.Name + FieldName;
        LookupDataSet := clansDataSet;
        LookupKeyFields := 'family_id';
        LookupResultField := 'family_group';
        DataSet := self.dataSet;
   end;


I've also tried applying these properties directly on the the dataSet, acting on the corresponding field ("character_family") or even adding a new one, with the same result. Maybe there's some other way ?

Code: [Select]

FieldTEmp := TStringField.Create(dataset);
   with FieldTEmp do
   begin
        FieldName := 'NewField';
        FieldKind := fkLookup;
        KeyFields := 'character_family';
        LookupDataSet := clansDataSet;
        LookupKeyFields := 'family_id';
        LookupResultField := 'family_group';
        DataSet := self.dataSet;
   end;
   self.dataSet.FieldDefs.Add(FieldTemp.FieldName, ftString, 50, false);
   self.dataSet.Fields.Add(FieldTEmp);


When I try to affect this field, the "TDBEdit" binded to other values of the table "Characters" don't work anymore, I get a runtime error : "EDataBaseError : character_name doesn't exist". It seems that creating a lookup field prevents controls to "acknowledge" the other fields of my table...

If a good soul could take me out of this hell, I would be very thankful :D

Thanks in advance to all :)

LuizAmérico

  • Sr. Member
  • ****
  • Posts: 457
RE: Lookup field in TDBGrid with TSQlite3DataSet
« Reply #1 on: September 16, 2008, 03:29:21 am »
When loading the data do:

  Dataset.Close;
  Dataset.Sql := 'Select character_name, character_firstName, family_group from Family, Character where family_id = character_family'
  Dataset.Open;

The sql may be wrong since i don't have a datafile to test.

 

TinyPortal © 2005-2018