在下面的过程中,通过连接到 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' 和 'name_2' 不接近,在调试时我发现适当的值是空的,因此我在模块 'GUI.exe' 中的地址 00822135 的访问冲突中遇到了问题。阅读地址 00000040)。