访问表字段 [英] Access to table fields

查看:93
本文介绍了访问表字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下过程中,通过与Delphi XE3中SQLite数据库的常规连接方式,我对数据库执行SQL查询,该查询包含具有来自不同表的相同名称的字段。然后,我通过别名寻址该字段

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;

但是通过调用此过程,我收到了表名未找到的消息。如何解决重复名称没有问题的字段? ('name_1'和'name
_2'不能接近,调试时我发现适当的值是空的,因此,我对模块 GUI.exe中地址00822135的访问冲突。读取地址00000040 )。

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 ).

推荐答案

您有错字(用tablemame代替表名):

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 ' [...]

这篇关于访问表字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆