add fields to existing dbf

Post Reply
jds
Posts: 106
Joined: Sat Dec 05, 2009 12:44 pm

add fields to existing dbf

Post by jds »

How can I simply add a field to an existing database with many fields
User avatar
karinha
Posts: 4882
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: add fields to existing dbf

Post by karinha »

Use google traductor:

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
 
Regards, saludos.
João Santos - São Paulo - Brasil
jds
Posts: 106
Joined: Sat Dec 05, 2009 12:44 pm

Re: add fields to existing dbf

Post by jds »

Thanks, I will try it...
Obrigado
jds
Posts: 106
Joined: Sat Dec 05, 2009 12:44 pm

Re: add fields to existing dbf

Post by jds »

The simpliest way to add a new field to an existing dbf is as follows:

local cgroep := space( 17 )
local oBrush, oBar, oBmp
local cOldBmp := GetProfString( "Desktop", "WallPaper" )
local oPrn, fntArial1, nRow, fntArial2, cName, cLoca, cPost
local nRecNo := RecNo()
local cTitle:= "Sociale Kruidenier Management - Field"
local cText:= " veldnaam(max10)+N of C+99+9 ?"
local cBmpfile:= "question.bmp"

set filter to
SetDeskWallPaper( "Fivewin.bmp" )
SetHandleCount(75) // to open until 75 files at a time

set century on
set date british
set epoch to 1950
set deleted on

if MsgGet( ctitle, cText, @cgroep, cBmpfile)

cnaam=LEFT(cgroep,10)
cchar=SUBSTR(cgroep,12,1)
clenght=SUBSTR(cgroep,14,2)
cdecim=RIGHT(cgroep,1)

use verkoop
copy to newverkoop structure extended

use newverkoop
replace field_name with cnaam
replace field_type with cchar
replace field_dec with VAL(cdecim)
replace field_len with VAL(clenght)

create another from newverkoop

use another
append from verkoop

delete file verkoop.dbf
use another
copy to verkoop

endif
User avatar
TimStone
Posts: 2536
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA
Contact:

Re: add fields to existing dbf

Post by TimStone »

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.
Tim Stone
http://www.MasterLinkSoftware.com
timstone@masterlinksoftware.com
Using: FWH 19.06 with Harbour 3.2.0 / Microsoft Visual Studio Community 2019
Horizon
Posts: 997
Joined: Fri May 23, 2008 1:33 pm

Re: add fields to existing dbf

Post by Horizon »

TimStone 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.
I also use Tim's method. but I do not rename original file name at first
1. I create the new file with temp filename.
2. I append records to new file from original file.
3. If any error is not occured, delete the original file, and rename the tempfile to original file.
Regards,

Hakan ONEMLI

Harbour & VS 2019 & FWH 20.12
User avatar
TimStone
Posts: 2536
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA
Contact:

Re: add fields to existing dbf

Post by TimStone »

The reason I don't delete the old file is it often takes the client awhile to realize not all of the data is present. By having the old file I can append in the missing data quickly.
Tim Stone
http://www.MasterLinkSoftware.com
timstone@masterlinksoftware.com
Using: FWH 19.06 with Harbour 3.2.0 / Microsoft Visual Studio Community 2019
jds
Posts: 106
Joined: Sat Dec 05, 2009 12:44 pm

Re: add fields to existing dbf

Post by jds »

Thank you Tim
Post Reply