add fields to existing dbf
Posted: Tue Jul 21, 2020 1:46 pm
How can I simply add a field to an existing database with many fields
www.FiveTechSoft.com
https://fivetechsoft.com/forums/
Code: Select all
#include "FiveWin.Ch"
FUNCTION ADD_FIELD( oMeter, oText, oDlg, lEnd )
FIELD AGE_CODIGO, AGE_GRUPO
LOCAL cChave
LOCAL nTotal := 100
LOCAL DEONDEVEM, lExiste, cAlias, nConta
oMeter:Refresh()
SysRefresh()
DBCLOSEALL()
IF FILE( "AGENDTEL.DBF" )
USE AGENDTEL ALIAS AGENDTEL EXCLUSIVE NEW
GO TOP
lExiste := .F.
cAlias := ALIAS()
FOR nConta := 1 TO ( cAlias )->( fCount() )
//-> Se o campo nao existir
IF ( cAlias )->( FieldName( nConta ) ) = "AGE_FONE3"
lExiste := .T.
EXIT
ENDIF
NEXT
//-> Para Mudar a Estrutura do Banco
//-> Neste Caso, Vou Mudar o CAMPO CODFORN Para 50
//-> IF LEN( AGENDTEL->AGE_NOME ) < 50
IF ! lExiste
DBCLOSEALL()
IF !FILE( "AGECOP02.DBF" )
SndPlaySound( ".\Erro.wav", 0 )
BEEP( 3 )
BEEP( 4 )
IF MsgNoYes( OemToAnsi( "ATEN€ÇO USUµRIO: " ) + CRLF + ;
OemToAnsi( " " ) + CRLF + ;
OemToAnsi( "O PROGRAMA DETECTOU NECESSIDADE " ) + CRLF + ;
OemToAnsi( "DE ATUALIZAۂO DO BANCO DE DADOS " ) + CRLF + ;
OemToAnsi( "DA AGENDA ELETRâNICA. " ) + CRLF + ;
OemToAnsi( "NECESSITA CRIAR _. " ) + CRLF + ;
OemToAnsi( "NECESSARIOS NESTA VERSÇO. " ) + CRLF + ;
OemToAnsi( " " ) + CRLF + ;
OemToAnsi( "ESTE PROCEDIMENTO, SOMENTE PODERµ" ) + CRLF + ;
OemToAnsi( "SER EFETUADO, NO SERVIDOR, OU " ) + CRLF + ;
OemToAnsi( "SE ESTIVER UTILIZANDO O PROGRAMA " ) + CRLF + ;
OemToAnsi( "FORA DA REDE. CUIDADO, PERIGO! " ) + CRLF + ;
OemToAnsi( " " ) + CRLF + ;
OemToAnsi( "SE ESTIVER EM UM TERMINAL, POR " ) + CRLF + ;
OemToAnsi( "FAVOR, SAIA DO PROGRAMA, E CHAME " ) + CRLF + ;
OemToAnsi( "O SUPERVISOR, E PE€A PARA O MESMO" ) + CRLF + ;
OemToAnsi( "FAZER ESTE PROCEDIMENTO DIRETO NO" ) + CRLF + ;
OemToAnsi( "SERVIDOR DE ARQUIVOS. " ) + CRLF + ;
OemToAnsi( " " ) + CRLF + ;
OemToAnsi( "FA€A UM BACKUP DE SEGURAN€A ANTES" ) + CRLF + ;
OemToAnsi( " " ) + CRLF + ;
OemToAnsi( "PROCEDIMENTO PERIGOSO EM AMBIENTE" ) + CRLF + ;
OemToAnsi( "DE REDE... NÇO INSISTA! RETORNE! " ) + CRLF + ;
OemToAnsi( " " ) + CRLF + ;
OemToAnsi( "<S>IM -> Para Executar Rotina " ) + CRLF + ;
OemToAnsi( "<N>ÇO -> Para Retornar. " ) , ;
OemToAnsi( "Atualiza‡Æo do Banco AgendTel.Dbf." ) ) = .F.
DBCLOSEALL()
__QUIT()
ENDIF
DBCLOSEALL()
IF FILE( "AGENDTEL.CDX" )
AEVAL( DIRECTORY( "AGENDTEL.CDX" ), { |aFILE| FERASE( aFILE[F_NAME] ) } )
ENDIF
oText:SetText( "Processando, Aguarde... Processo Demorado " )
DbCreate( "AGECOP02.DBF", { { "AGE_CODIGO", "C", 06, 00 }, ;
{ "AGE_APELID", "C", 25, 00 }, ;
{ "AGE_NOME", "C", 50, 00 }, ;
{ "AGE_GRUPO", "C", 03, 00 }, ;
{ "AGE_DESCGR", "C", 30, 00 }, ;
{ "AGE_NIVER", "D", 08, 00 }, ;
{ "AGE_ENDERE", "C", 40, 00 }, ;
{ "AGE_COMPLE", "C", 25, 00 }, ;
{ "AGE_BAIRRO", "C", 25, 00 }, ;
{ "AGE_CIDADE", "C", 25, 00 }, ;
{ "AGE_UF", "C", 02, 00 }, ;
{ "AGE_CEP", "C", 10, 00 }, ;
{ "AGE_FONE", "C", 15, 00 }, ;
{ "AGE_DDD1", "C", 02, 00 }, ;
{ "AGE_COMRE1", "C", 01, 00 }, ;
{ "AGE_FONE2", "C", 15, 00 }, ;
{ "AGE_DDD2", "C", 02, 00 }, ;
{ "AGE_COMRE2", "C", 01, 00 }, ;
{ "AGE_FONE3", "C", 15, 00 }, ;
{ "AGE_DDD5", "C", 02, 00 }, ;
{ "AGE_COMRE5", "C", 01, 00 }, ;
{ "AGE_CELULA", "C", 15, 00 }, ;
{ "AGE_DDD3", "C", 02, 00 }, ;
{ "AGE_FAX", "C", 15, 00 }, ;
{ "AGE_DDD4", "C", 02, 00 }, ;
{ "AGE_CADAST", "D", 08, 00 }, ;
{ "AGE_EMAIL", "C", 50, 00 }, ;
{ "AGE_HOMEP", "C", 50, 00 }, ;
{ "AGE_CONTAT", "C", 26, 00 }, ;
{ "AGE_OBS1", "C", 50, 00 }, ;
{ "AGE_OBS2", "C", 50, 00 }, ;
{ "AGE_OBS3", "C", 50, 00 }, ;
{ "AGE_OBS4", "C", 50, 00 } } )
DbCloseAll()
USE AGECOP02 ALIAS AGECOP02 EXCLUSIVE // C¢pia de AGENDTEL.DBF
GO TOP
// Se Existe AGENDTEL.DBF no Diretorio C:\AGENDA
// AGENDTEL Antigo
IF File( "AGENDTEL.DBF" )
oText:SetText( "Processando, Aguarde... Banco do AGENDTEL " )
APPEND FROM AGENDTEL.DBF
// Se Existe AGENDTEL.DBF
COPY TO AGENDTEL.DBF
ELSE
oText:SetText( "Processando, Aguarde... Banco do AGENDTEL " )
COPY TO AGENDTEL.DBF // Se NÆo Existe AGENDTEL.DBF
ENDIF
DBCLOSEALL()
IF NetUse( "AGENDTEL", .F. )
oMeter:nTotal = RecCount()
INDEX ON VAL( AGE_CODIGO ) TAG CODIGO TO AGENDTEL ;
FOR !DELETED() ;
EVAL ( oMeter:Set( RecNo() ), CursorWait(), SysRefresh() ) ;
EVERY 10
INDEX ON Field->AGE_NOME TAG NOME TO AGENDTEL ;
FOR !DELETED() ;
EVAL ( oMeter:Set( RecNo() ), CursorWait(), SysRefresh() ) ;
EVERY 10
INDEX ON Field->AGE_APELID TAG APELIDO TO AGENDTEL ;
FOR !DELETED() ;
EVAL ( oMeter:Set( RecNo() ), CursorWait(), SysRefresh() ) ;
EVERY 10
INDEX ON VAL( AGE_GRUPO ) TAG GRUPO TO AGENDTEL ;
FOR !DELETED() ;
EVAL ( oMeter:Set( RecNo() ), CursorWait(), SysRefresh() ) ;
EVERY 10
ENDIF
DbCloseAll()
ENDIF
ENDIF
ENDIF
RETURN NIL
I also use Tim's method. but I do not rename original file name at firstTimStone wrote:I simply rename the original file, then create a new one ( perhaps with many modifications ) using the original name, then append from the old one.
I do NOT delete the renamed file in case something happens during the append which results in not all data being copied.
The method is safe and very fast.
With my application, I have a data files version number. My applcation checks to see if a new version number was issued, and then runs a built in routine which will see what files need to be updated ( either because the field count changed, or the file is new ), and takes care of this.. It also rebuilds the indexes. It's fast, automated, and pretty transparent to the user.