MODIFY A DBF STRUCTURE

HMG en Español

Moderator: Rathinagiri

Post Reply
User avatar
fouednoomen
Posts: 172
Joined: Sun Oct 14, 2012 8:33 am
DBs Used: DBF, MySQL, MariaDB, SQLite, PostgreSQL, Oracle, ODBC
Location: Tunisia
Been thanked: 9 times

MODIFY A DBF STRUCTURE

Post by fouednoomen »

Hi all Friends.
I need a very simple code to add a field to dbf file

Many thanks

martingz
Posts: 342
Joined: Wed Nov 18, 2009 11:14 pm
Location: Mexico
Has thanked: 61 times
Been thanked: 36 times

Post by martingz »

Fouednoomen i use this meny year ago, i hope help you

dbstrct:={{"CLAVE","N",5,0},{"NOMBRE","C",100,0},{"RFC","C",15,0},{"DOMICILIO","C",100,0},{"NOEXT","C",20,0},{"NOINT","C",20,0},{"COLONIA","C",70,0},{"CIUDAD","C",70,0},{"ESTADO","C",50,0},;
{"TELEFONO","C",30,0},{"FAX","C",10,0},{"CONTACTO","C",60,0},{"TELEFONO1","C",30,0},{"TELEFONO2","C",30,0},{"EMAIL","C",70,0},{"ENVIARXML","L",1,0},{"PAIS","C",20,0},{"CP","N",5,0},;
{"MEPAGO","N",2,0},{"NUCUENTA","N",25,0}}

select cliente
dbstract:=dbStruct()
if len(dbstrct) > len(dbstract)
creafiles("cliente","cliente.dbf")
if !abrearch('1',"cliente","cliente",{"cliente"},1,1)
MsgInfo('La base de datos de '+"Clientes"+' esta en uso exclusivo','Mensaje del Sistema')
retvalarch:=.f.
endif
endif

function creafiles
parameters malias,mbasedbf
vardbdif:=varpath + "\tempdbf.dbf"
vargendbf:=GetCurrentFolder() + "\datos\" + mbasedbf
FILECOPY(mbasedbf, mbasedbf+".bak", ,_SMALL_BLOCK, Nil)
dbCreate( vargendbf , dbstrct, "DBFCDX", .T., "Mientras" )
select mientras
close
dbCreate( vardbdif , dbstrct, "DBFCDX", .T., "MYALIAS" )
select myalias
nfil:=varpath + "\" + mbasedbf
append from &(nfil)
select myalias
close
DBSELECTAREA( MALIAS )
close
delete file &(nfil)
rename (vardbdif) to (nfil)
fpttemp:=substr(vardbdif,1,len(vardbdif)-4)+'.fpt'
fptact:=substr(nfil,1,len(nfil)-4)+'.fpt'
if file(fpttemp)
delete file &(fptact)
rename (fpttemp) to (fptact)
endif
return Nil

Function FILECOPY(cSource, cDestination, nBuffer, bBlock)
Local sourceHandle, destHandle, lSuccess:= .F., TmpBuff, LastPos
Local BuffPos, ByteCount, cBType:= ValType(bBlock)

Default nBuffer := 8192

If ( (sourceHandle:= fopen(cSource, 0)) != -1 )
If ( (destHandle:= fcreate(cDestination, 0)) != -1 )
LastPos:= fseek(sourceHandle, 0, 2)
BuffPos:= 0
ByteCount:= 0
fseek(sourceHandle, 0, 0)
Do While (BuffPos < LastPos)
TmpBuff := Space(nBuffer)
BuffPos += (ByteCount:= fread(sourceHandle, @TmpBuff, nBuffer))
fwrite(destHandle, TmpBuff, ByteCount)
If cBType == "B"
eval(bBlock, BuffPos / LastPos)
EndIf
EndDo
lSuccess:= fclose(destHandle)
EndIf
fclose(sourceHandle)
EndIf
inkey(1)
Return lSuccess

sorry for my bad english

franco
Posts: 625
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Been thanked: 47 times

Post by franco »

Hi Fouednoomen,
At the start of my programs I usually create my tables for the program and a folder for temporary indexes.
The temp folder is mainly for temp indexes. It is in the C drive so whether on a server or terminal the temp indexes
and other temp files are local. This really speeds up the program.
If I have a table CUSTOMER with fields
{'NAME' ,'C' , 30,0}
{'CITY' ,'C' , 30,0} **** 2 fields
And I want to add phone
At start of my main program I check for or create tables Like:

Code: Select all

procedure main
Local and Public
createfolder('c:\MYTEMP')                      // IF IT EXISTS IT WILL DISREGARD
Tables()
da. da. da
return
function Tables
Local CHSTRUCTURE := .F.
IF FILE('CUSTOMER.DBF') 
USE  CUSTOMER NEW SHARED
           
	IF FCOUNT() <> 3           // I  WANT TO ADD 1
		USE
		copy file CUSTOMER.dbf to C:\MYTEMP\CUSTOMER1.DBF       //Had error in original post Copy file name was wrong.
		CHSTRUCT := .T.
		msgbox('Structure Will Be Changed')
	ENDIF
	close all
ENDIF


  IF ! FILE('CUSTOMER.DBF') .OR. CHSTRUCTURE = .T.
	CF := {}
	aADD(CF,{'NAME'    ,'C' , 30,0})
	aADD(CF,{'CITY'      ,'C' , 30,0})
	aADD(CF,{'PHONE'  ,'C' , 14,0})    //3 fields
	DBCREATE('acmotor.dbf',CF)
	USE
	select 0
	use CUSTOMER new                               //shared
	index on NAME to NAME
	IF CHSTRUCTURE = .T.
		APPEND FROM C:\MYTEMP\CUSTOMER1.DBF
		CHSTRUCTURE := .F.
	ENDIF

	close all
 ENDIF
RETURN
I do not erase temp file as it is there for a backup if needed.
All The Best,
Franco
Canada

Post Reply