Access to table fields

喜欢而已 提交于 2019-12-11 10:40:05

问题


In the following procedure by means of a conventional component of connection to the SQLite databases in Delphi XE3 I execute a SQL query to a database, containing fields with identical names from different tables. Then I address to this field through alias

procedure CreationListOfFields(SQLConn: TSQLConnection; DBSchema : TDBSchemaSpec);
var
  NameField : TField;
  PositionField : TField;
  DescriptionField : TField;
  CanInputField : TField;
  CanEditField : TField;
  ShowInGridField : TField;
  ShowInDetailsField : TField;
  IsMeanField : TField;
  AutocalculatedField : TField;
  RequiredField : TField;
  Name1 : TField;
  Name2 : TField;
begin
    SQLConn.Execute('select f.id, f.position, f.name, f.description, f.can_input, '
    +' f.can_edit, f.show_in_grid, f.show_in_details, f.is_mean, f.autocalculated, f.required, tables.name tablename, domains.name domainname'
    +' from fields f left join tables on f.table_id=tables.id '
    +' left join domains on f.domain_id=domains.id order by tables.name, domains.name ', nil, results);
    if not results.IsEmpty then
      begin
        results.First;
        Name1:=results.FieldByName('tablename');
        Name2:=results.FieldByName('domainname');
        lastTable:=Name1.AsString;
        TableSpec:=TTableSpec(DBSchema.Tables.FindComponent(lastTable));
        lastDomain:=Name2.AsString;
        DomainSpec:=TDomainSpec(DBSchema.Domains.FindComponent(lastDomain));
        NameField:=results.FieldByName('name');
        PositionField:=results.FieldByName('position');
        DescriptionField:=results.FieldByName('description');
        CanInputField:=results.FieldByName('can_input');
        CanEditField:=results.FieldByName('can_edit');
        ShowInGridField:=results.FieldByName('show_in_grid');
        ShowInDetailsField:=results.FieldByName('show_in_details');
        IsMeanField:=results.FieldByName('is_mean');
        AutocalculatedField:=results.FieldByName('autocalculated');
        RequiredField:=results.FieldByName('required');
        while not results.Eof do
          begin
            if (Name1.AsString<>lastTable) then
            begin
              lastTable:=Name1.AsString;
              TableSpec:=TTableSpec(DBSchema.Tables.FindComponent(lastTable));
            end;
            if (Name2.AsString<>lastDomain) then
            begin
              lastDomain:=Name2.AsString;
              DomainSpec:=TDomainSpec(DBSchema.Domains.FindComponent(lastDomain));
            end;
            FieldSpec:=TFieldSpec.Create(TableSpec.Fields);
            FieldSpec.Setup( DomainSpec, PositionField.AsInteger,
            NameField.AsString, DescriptionField.AsString,
            FieldToBoolean(CanInputField),FieldToBoolean(CanEditField),
            FieldToBoolean(ShowInGridField), FieldToBoolean(ShowInDetailsField),
            FieldToBoolean(IsMeanField),FieldToBoolean(AutocalculatedField),
            FieldToBoolean(RequiredField));
            TComponent(FieldSpec).Name:=NameField.AsString;
            TableSpec.Fields.InsertComponent(FieldSpec);
            results.Next;
          end;
      end;
end;

But by a call of this procedure as a result I receive Field 'tablename' not found message. How to address to fields with repeating names that there were no problems? ('name_1' and 'name _2' don't approach, when debugging I found out that appropriate values empty and as a result because of it I had a question on Access violation at address 00822135 in module 'GUI.exe'.Read of address 00000040 ).


回答1:


You have a typo (tablemame instead of tablename):

[...], f.autocalculated, f.required, tables.name table->m<-ame, domains.name domainname'
+' from fields f left join tables on f.table_id=tables.id ' [...]


来源:https://stackoverflow.com/questions/14032094/access-to-table-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!