FTP | CCD | Buscar | Trucos | Trabajo | Foros |
|
Registrarse | FAQ | Miembros | Calendario | Guía de estilo | Buscar | Temas de Hoy | Marcar Foros Como Leídos |
|
Herramientas | Buscar en Tema | Desplegado |
#1
|
||||
|
||||
Agregar un campo a una tabla paradox (runtime)
Saludos a todos.
Tengo una aplicación que utiliza BDD Paradox y por supuesto se me olvidó agregar un simple campo en una de las bases de datos, ya que no puedo ir de equipo en equipo cambiando la estructura, necesito agragarle al programa la capacidad de comprobar la existencia de un campo y si no existe que lo agregue en tiempo de ejecución. Para eso agregué el siguiente código (tomado de: http://www.experts-exchange.com/Comp..._20664695.html ) Var db:TTable; Begin db := tTable.Create(nil); db.DatabaseName := dbTipo06.DatabaseName; db.TableName := dbTipo06.TableName; db.open; If db.FindField('NombreCampoOlvidado') = nil Then Begin db.close; AddField(db,'NombreCampoOlvidado',6,0,4); // Campo Integer End; db.Free; End; y el procedimiento AddField: unit AddFields; interface Uses DBTables, Bde, SysUtils; { Definiciones de tipo y subtipo String: 1,0 Time: 10,0 TimeStamp: 11,0 Date: 2,0 Memo: 3,22 Binary: 3,23 Formatted Memo: 3,24 OLE: 3,25 Graphics: 3,26 Boolean: 4,0 Small Integer: 5,0 Integer: 6,0 AutoInc: 6,29 Float: 7,0 Currency: 7,21 BCD: 8,0 Byte: 9,0 } procedure AddField(Table : TTable; FldName : String;FldType, FldSubType,FldSize:Integer); implementation procedure AddField(Table : TTable; FldName : String;FldType, FldSubType,FldSize:Integer); type FLDDescs = array [1..100] of FLDDesc; // these are kludges for ease of addressing PFLDDescs = ^FLDDescs; CROpTypes = array [1..100] of CROpType; PCROpTypes = ^CROpTypes; var Props: CURProps; hDb: hDBIDb; TableDesc: CRTblDesc; pFields: pFLDDescs; pOp: pCROpTypes; i:Integer; begin // Make sure table is opened exclusively If (Table.Active and Not Table.Exclusive) Then Table.Close; If (Not Table.Exclusive) Then Table.Exclusive := True; If (Not Table.Active) Then Table.Open; try Check(DbiGetCursorProps(Table.Handle, Props)); pFields := AllocMem((props.iFields+1) * sizeof(FLDDesc)); pOp := AllocMem((props.iFields+1) * sizeof(CROpType)); Check(DbiGetFieldDescs(table.Handle,@PFields[1])); for i := 1 to props.iFields do // retain existing fields pop^[i] := crNOOP; i := props.iFields + 1; pOp^[i] := crADD; with PFields^[i] do begin StrCopy(szName,PChar(FldName)); iFldType := FldType; ISubType := FldSubType; IUnits1 := FldSize; end; FillChar(tabledesc,sizeof(CRTblDesc),#0); with tabledesc do begin StrPCopy(szTblName,Table.TableName); StrCopy(szTblType,szParadox); bPack := True; iFldCount := props.iFields+1; pecrFldOp := @pop^[1]; pfldDesc := @pfields^[1]; end; Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb))); Table.Close;// table must be closed as restructure requires exclusive access Check(DbiDoRestructure(hdb,1,@tabledesc,nil,nil,nil,False)); finally FreeMem(PFields); FreeMem(pOp); end; end; end. Todo bien en teoría, pero no funciona porque me genera una excepción: "Invalid handle to the functión". Lo mismo me sucedió con varios métodos "AddField" que usan una tecnica similar (incluyendo uno tomado directamente de borland). ¿Alguien sabe porque ocurre este error y como solucionarlo? PD. Uso Delphi 7. Gracias una vez más.
__________________
Sitrico |
Herramientas | Buscar en Tema |
Desplegado | |
|
|
|