Q&A

  • 테이블구조를 동적으로 변경하고 싶어요...
디비는 파라독스를 사용하고 있는데

테이블에 필드타잎을 +(자동증가)로 했는데 문제가 생겨

Integer로 바꿀려고 하는데 Desktop에서 변경하면 되는데

사정상 프로그램으로 변경을 해야합니다.

여기저기 알아보니 DbiDoRestructure함수로 가능하다는데

사용방법을 모르겠읍니다.

아시는분 계시면 제발 부탁드립니다.

2  COMMENTS
  • Profile
    배경 2000.11.25 03:08
    박윤정 wrote:

    > 디비는 파라독스를 사용하고 있는데

    > 테이블에 필드타잎을 +(자동증가)로 했는데 문제가 생겨

    > Integer로 바꿀려고 하는데 Desktop에서 변경하면 되는데

    > 사정상 프로그램으로 변경을 해야합니다.

    > 여기저기 알아보니 DbiDoRestructure함수로 가능하다는데

    > 사용방법을 모르겠읍니다.

    > 아시는분 계시면 제발 부탁드립니다.





    ============================================================================

    > ////자동즐가 필드 만들기 소스

    > >

    > > procedure TF_varim.BitB_tc5Click(Sender: TObject);

    > > var

    > > Tablename:string; a,i,count:Integer;

    > > Itemlist: TStringList;

    > >

    > > begin

    > >

    > > //------------------------------------------------------------------------------//

    > > //------------------------------------------------------------------------------//

    > > with Dm.Q_basepay do

    > > begin

    > > close; //다른 테이블으 레코드 수를 체크하느 루틴

    > > sql.Clear;

    > > sql.add('select var_div from pyvarim');

    > > sql.add('where var_div="E"');

    > > active:=true;

    > > end;

    > >

    > > a := Dm.Q_basepay.RecordCounT;

    > >

    > > With Dm.T_ptaxm do begin // 테이블의 존재 유무를 검사

    > > Itemlist:=TStringList.Create;

    > > Session.GetTableNames('Pay2000','',False,False,ItemList);

    > > If Itemlist.IndexOf(Tablename)>=0 then

    > > Begin

    > > Itemlist.free;

    > > exit;

    > > end;

    > > Itemlist.free;

    > >

    > > Close;

    > > Tablename:='pyptaxm';

    > > Tabletype:=ttParadox;

    > >

    > > with FieldDefs do begin

    > > clear;

    > > Add('Ptax_suppdiv',ftString,1,False); //지급구분

    > > Add('Ptax_suppym',ftInteger,0,False); //지급년도

    > > Add('Ptax_codenum',ftString,8,False); //사원번호

    > > //테이블 안의 필드를 위 의 다른 테이블 레코드 수만큼 증가시킨다

    > > // 이때 다른 테이블의 레코드 즉 자료의 수가 변동이 있을때 문제임

    > > // 기존자료는 보존하고 새로운 필드만 추가해야함

    > > FOR i := 1 TO a do

    > > begin

    > > Add('ptax_comm'+INTTOSTR(i),ftFloat,0,True); //공제항목

    > > end;

    > > end;

    > >

    > > with IndexDefs do

    > > begin

    > > clear; //필드정의 각각

    > > Add('Field1Index','Ptax_suppdiv;Ptax_suppym;Ptax_codenum',[ixPrimary,ixUnique]); //필드의 인덱스 정의

    > > end;

    > > CreateTable;

    > > open;

    > > end;

    > >

    > > //위에서 정의된 필드의 디비그리드의 칼럼의 내용 이름 필등명 색등을 정의한다

    > > >

    > > > 소스는 ========================================================

    > > > new_column := DG_Temp.columns.Add; //부서적요 field

    > > > new_column.FieldName := 'jukyo';

    > > > new_column.Color := cl3DLight;

    > > > New_column.AlwaysShowEditButton := True;

    > > > New_column.EndEllipsis := True;

    > > > New_column.ButtonStyle := cbsEllipsis;

    > > > ===============================================================

    > > >

    > >

    > >

    > > 아래와 같이 Clear를 사용해 보세요.

    > > with DBGrid1 do begin

    > > Columns.BeginUpdate;

    > > Columns.Clear;

    > > Columns.Add;

    > > Columns[0].FieldName := 'First';

    > > Columns[0].Title.Caption := '첫번째필드';

    > > Columns[0].Width := 130;

    > > Columns[0].Color := $00D0FFFF;

    > > Columns.Add;

    > > Columns[1].FieldName := 'First';

    > > Columns[1].Title.Caption := '두번째필드';

    > > Columns[1].Width := 130;

    > > Columns[1].Color := $00D0FFFF;

    > >

    > > 추가하고픈 만큼 추가...

    > >

    > > Columns.EndUpdate;

    > > end;

    > >



  • Profile
    s6763 2000.11.24 18:03
    박윤정 wrote:

    > 디비는 파라독스를 사용하고 있는데

    > 테이블에 필드타잎을 +(자동증가)로 했는데 문제가 생겨

    > Integer로 바꿀려고 하는데 Desktop에서 변경하면 되는데

    > 사정상 프로그램으로 변경을 해야합니다.

    > 여기저기 알아보니 DbiDoRestructure함수로 가능하다는데

    > 사용방법을 모르겠읍니다.

    > 아시는분 계시면 제발 부탁드립니다.







    아래는 BDE32.HLP에 나와있는 예제입니다.

    테이블 구조 변경하는 루틴인데, 아래내용을 복사하여 델파이에서 이용해 보세요;



    Example 3: Alter a field in a Paradox or dBASE table.



    This example will alter an existing field in a Paradox or dBASE table. NOTE: You must fill in all options in the ChangeRec with 0 or '' if the option is not used in the restructure. FillChar can be used to do this:



    Fillchar(MyChangeRec, sizeof(MyChangeRec), 0);



    This example uses the following input:



    ChangeField(Table1, Table1.FieldByName('FOO'), MyChangeRec)



    ChangeRec is defined as follows:



    type



    ChangeRec = packed record

    szName: DBINAME;

    iType: Word;

    iSubType: Word;

    iLength: Word;

    iPrecision: Byte;

    end;





    The function is defined as follows:



    procedure ChangeField(Table: TTable; Field: TField; Rec: ChangeRec);



    var

    Props: CURProps;

    hDb: hDBIDb;

    TableDesc: CRTblDesc;

    pFields: pFLDDesc;

    pOp: pCROpType;

    B: Byte;

    begin

    // Initialize the pointers...

    pFields := nil;

    pOp := nil;

    // Make sure the table is open exclusively so we can get the db handle...

    if not Table.Active then

    raise EDatabaseError.Create('Table must be opened to restructure');

    if not Table.Exclusive then

    raise EDatabaseError.Create('Table must be opened exclusively' +



    'to restructure');

    Check(DbiSetProp(hDBIObj(Table.Handle), curxltMODE, Integer(xltNONE)));

    // Get the table properties to determine table type...

    Check(DbiGetCursorProps(Table.Handle, Props));

    // Make sure the table is either Paradox or dBASE...

    if (Props.szTableType <> szPARADOX) and (Props.szTableType <> szDBASE) then

    raise EDatabaseError.Create('Field altering can only occur on Paradox' +

    ' or dBASE tables');



    // Allocate memory for the field descriptor...

    pFields := AllocMem(Table.FieldCount * sizeof(FLDDesc));

    // Allocate memory for the operation descriptor...

    pOp := AllocMem(Table.FieldCount * sizeof(CROpType));

    try

    // Set the pointer to the index in the operation descriptor to put

    // crMODIFY (This means a modification to the record is going to happen)...

    Inc(pOp, Field.Index);

    pOp^ := crMODIFY;

    Dec(pOp, Field.Index);

    // Fill the field descriptor with the existing field information...



    Check(DbiGetFieldDescs(Table.Handle, pFields));

    // Set the pointer to the index in the field descriptor to make the

    // midifications to the field

    Inc(pFields, Field.Index);

    // If the szName portion of the ChangeRec has something in it, change it...

    if (Length(Rec.szName) > 0) then

    pFields^.szName := Rec.szName;

    // If the iType portion of the ChangeRec has something in it, change it...

    if (Rec.iType > 0) then



    pFields^.iFldType := Rec.iType;

    // If the iSubType portion of the ChangeRec has something in it, change it...

    if (Rec.iSubType > 0) then

    pFields^.iSubType := Rec.iSubType;

    // If the iLength portion of the ChangeRec has something in it, change it...

    if (Rec.iLength > 0) then

    pFields^.iUnits1 := Rec.iLength;

    // If the iPrecision portion of the ChangeRec has something

    // in it, change it...

    if (Rec.iPrecision > 0) then



    pFields^.iUnits2 := Rec.iPrecision;

    Dec(pFields, Field.Index);

    for B := 1 to Table.FieldCount do begin

    pFields^.iFldNum := B;

    Inc(pFields, 1);

    end;

    Dec(pFields, Table.FieldCount);



    // Blank out the structure...

    FillChar(TableDesc, sizeof(TableDesc), #0);

    // Get the database handle from the table's cursor handle...

    Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));



    // Put the table name in the table descriptor...

    StrPCopy(TableDesc.szTblName, Table.TableName);

    // Put the table type in the table descriptor...

    StrPCopy(TableDesc.szTblType, Props.szTableType);

    // The following three lines are necessary when doing any field restructure

    // operations on a table...



    // Set the field count for the table

    TableDesc.iFldCount := Table.FieldCount;

    // Link the operation descriptor to the table descriptor...



    TableDesc.pecrFldOp := pOp;

    // Link the field descriptor to the table descriptor...

    TableDesc.pFldDesc := pFields;

    // Close the table so the restructure can complete...

    Table.Close;

    // Call DbiDoRestructure...

    Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, False));

    finally

    if (pFields <> nil) then

    FreeMem(pFields);

    if (pOp <> nil) then



    FreeMem(pOp);

    end;

    end;