Модуль DBFAccess

Модуль можно скачать по этой ссылке (16,9 КиБ).

Модуль DBFAccess содержит определение констант (согласно описанию формата фалов dbf) и структур TDBF_HEAD, TDBF_FIELD_REC, описание и реализацию классов EDBFError, TDBF_fields, TDBF_fileMemo, TDBF_file. А также две функции StrWinToDos и StrDosToWin, переводящие строку из кодировки Windows (WIN1251) в кодировку DOS (CP866) и обратно.

При чтении информации из КЛАДР вся работа происходит с интерфесом объекта TDBF_file:

...
const c_ScName_SocrBase   =1;
         c_SocrName_SocrBase =2;

         c_Name_Kladr  =0;
         c_Socr_Kladr  =1;
         c_Code_Kladr  =2;
         c_Index_Kladr =3;

         c_Name_Street  =0;
         c_Socr_Street  =1;
         c_Code_Street  =2;
...

dbf_file :=TDBF_file.Create;
try
//работа с файлом socrbase.dbf
    dbf_file.Open(ПУТЬ К ФАЙЛУ SOCRBASE.DBF);
    try
      dbf_file.First;
      while not dbf_file.Eof do begin
        dbf_file.GetRecord;

        s :='INSERT INTO TableSocrForKladr(Socr,Name_) VALUES(''' +
             StrDosToWin(dbf_file.Field[c_ScName_SocrBase]) + ''',''' +
             StrDosToWin(dbf_file.Field[c_SocrName_SocrBase]) + ''')';

        ВЫПОЛНИТЬ КОМАНДУ INSERT
      end; //while
    finally
      dbf_file.Close;
    end;

//работа с файлом kladr.dbf
    dbf_file.Open(ПУТЬ К ФАЙЛУ KLADR.DBF);
    try

      dbf_file.First;
      while not dbf_file.Eof do begin
        dbf_file.GetRecord;
        if Copy(StrDosToWin(dbf_file.Field[c_Code_Kladr]),12,2) = '00' then begin
           s :='INSERT INTO TableKladr(K1,K2,K3,K4,Name_,Socr,PostIndex) VALUES(' +
                Copy(StrDosToWin(dbf_file.Field[c_Code_Kladr]),1,2) + ',' +
                Copy(StrDosToWin(dbf_file.Field[c_Code_Kladr]),3,3) + ',' +
                Copy(StrDosToWin(dbf_file.Field[c_Code_Kladr]),6,3) + ',' +
                Copy(StrDosToWin(dbf_file.Field[c_Code_Kladr]),9,3) + ',''' +
                StrDosToWin(dbf_file.Field[c_Name_Kladr]) + ''',''' +
                StrDosToWin(dbf_file.Field[c_Socr_Kladr]) + ''',';
           if trim(dbf_file.Field[c_Index_Kladr]) = ''
              then s :=s + 'NULL'
              else s :=s + StrDosToWin(dbf_file.Field[c_Index_Kladr]);
           s :=s + ')';

           ВЫПОЛНИТЬ КОМАНДУ INSERT
        end; //if
      end; //while

    finally
      dbf_file.Close;
    end;

//работа с файлом street.dbf
    dbf_file.Open(ПУТЬ К ФАЙЛУ STREET.DBF);
    try
      
dbf_file.First;
      while not dbf_file.Eof do begin
        dbf_file.GetRecord;
        if Copy(StrDosToWin(dbf_file.Field[c_Code_Street]),16,2) = '00' then begin
           s :='INSERT INTO TableStreets(K1,K2,K3,K4,K5,Name_,Socr) VALUES(' +
                Copy(StrDosToWin(dbf_file.Field[c_Code_Street]),1,2) + ',' +
                Copy(StrDosToWin(dbf_file.Field[c_Code_Street]),3,3) + ',' +
                Copy(StrDosToWin(dbf_file.Field[c_Code_Street]),6,3) + ',' +
                Copy(StrDosToWin(dbf_file.Field[c_Code_Street]),9,3) + ',' +
                Copy(StrDosToWin(dbf_file.Field[c_Code_Street]),12,4) + ',''' +
                StrDosToWin(dbf_file.Field[c_Name_Street]) + ''',''' +
                StrDosToWin(dbf_file.Field[c_Socr_Street]) + '''';
           s :=s + ')';

           ВЫПОЛНИТЬ КОМАНДУ INSERT
        end; //if
      end; //while

    finally
      dbf_file.Close;
    end;

finally
   dbf_file.Free;
end;

Добавить комментарий