访问表字段

时间:2012-12-25 15:53:18

标签: sql delphi sqlite delphi-xe2 field

在以下过程中,通过连接到Delphi XE3中的SQLite数据库的传统组件,我对数据库执行SQL查询,该数据库包含来自不同表的相同名称的字段。然后我通过别名

来解决这个问题
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;

但是通过调用此过程,我收到了Field'tablename'not found消息。如何处理重复名称没有问题的字段? ('name_1'和'名字 _2'不接近,在调试时我发现适当的值为空,因此我在Access violation at address 00822135 in module 'GUI.exe'.Read of address 00000040上有一个问题。)

1 个答案:

答案 0 :(得分:1)

你有一个拼写错误(tablemame而不是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 ' [...]