Paul E. Schoen
2010-07-07 20:59:59 UTC
I have a D4 Pro application that uses dBase files, and need to change the
structure of a table to add a new field and keep the contents of the
existing data. I planned to do so by supplying a new table "OrtRes1.dbf"
with the new structure and then import the data from the original
"OrtRes.dbf". For a new installation, I would just supply the new table and
if the old table is not present, the import would not be required.
At first I inserted a new field at a convenient location, but when I tried
to import the data by using BatchMove, it copied OK up to the new field and
then ignored the remaining fields. I also had problems using the batDelete
parameter. The only way I could find to handle this was to use
TBatchMove.Mappings to specify each field, which seemed cumbersome. So I
changed the structure so that the new field was at the end, and it seemed to
work OK:
If FileExists( OLDRESULTSFILE ) and FileExists( RESULTSFILE ) then begin
//1
if Application.MessageBox( 'Import old data from ' + OLDRESULTSFILE +
' to ' + RESULTSFILE + '? ', 'Import Results', MB_YESNO ) = ID_YES then
try //2
tblResOld := TTable.Create(self);
tblResOld.TableType := ttDBase;
tblResOld.TableName := OLDRESTABLENAME; // File name without
extension
tblResOld.Open;
CopyFile( RESULTSFILE, RESBACKUP, TRUE ); // Save new file
tblRes.EmptyTable; // Clear data
tblRes.BatchMove( tblResOld, batAppend ); // Update new file with old
data
tblResOld.Close;
if not RenameFile( OLDRESULTSFILE, OLDRESBACKUP ) then // Save
original file
Application.MessageBox( 'Error Renaming File', OLDRESULTSFILE,
MB_OK );
finally
tblRes.Close;
if tblResOld <> nil then
tblResOld.Free;
end; //-2
end; //-1
Another way I might handle this would be to check the structure of the
existing database and add the new field if not present. This would also
eliminate the need to change the filename. What is the "best" way to do
this?
Thanks,
Paul
structure of a table to add a new field and keep the contents of the
existing data. I planned to do so by supplying a new table "OrtRes1.dbf"
with the new structure and then import the data from the original
"OrtRes.dbf". For a new installation, I would just supply the new table and
if the old table is not present, the import would not be required.
At first I inserted a new field at a convenient location, but when I tried
to import the data by using BatchMove, it copied OK up to the new field and
then ignored the remaining fields. I also had problems using the batDelete
parameter. The only way I could find to handle this was to use
TBatchMove.Mappings to specify each field, which seemed cumbersome. So I
changed the structure so that the new field was at the end, and it seemed to
work OK:
If FileExists( OLDRESULTSFILE ) and FileExists( RESULTSFILE ) then begin
//1
if Application.MessageBox( 'Import old data from ' + OLDRESULTSFILE +
' to ' + RESULTSFILE + '? ', 'Import Results', MB_YESNO ) = ID_YES then
try //2
tblResOld := TTable.Create(self);
tblResOld.TableType := ttDBase;
tblResOld.TableName := OLDRESTABLENAME; // File name without
extension
tblResOld.Open;
CopyFile( RESULTSFILE, RESBACKUP, TRUE ); // Save new file
tblRes.EmptyTable; // Clear data
tblRes.BatchMove( tblResOld, batAppend ); // Update new file with old
data
tblResOld.Close;
if not RenameFile( OLDRESULTSFILE, OLDRESBACKUP ) then // Save
original file
Application.MessageBox( 'Error Renaming File', OLDRESULTSFILE,
MB_OK );
finally
tblRes.Close;
if tblResOld <> nil then
tblResOld.Free;
end; //-2
end; //-1
Another way I might handle this would be to check the structure of the
existing database and add the new field if not present. This would also
eliminate the need to change the filename. What is the "best" way to do
this?
Thanks,
Paul