Changing Field Names in Database - Looking 4 code 
Author Message
 Changing Field Names in Database - Looking 4 code

Does anyone have any tips or code that I can use to change the name of
fields in a database?

I have a 3rd party app that exports to a database.  To be able to append the
.DBF output into my program, I need to change 3 of the field names.

I know I could always write and SDF export, and import into a database with
the desired field names, but I was wondering if anyone has code that
directly and quickly changes fields names via low level file routines.

John



Mon, 21 Jul 2003 21:51:14 GMT  
 Changing Field Names in Database - Looking 4 code
Nevermind.

I was able to write a function that uses low level I/O to change the field
names.  I just had to refresh myself with the .DBF file structure.

I have attached the code for those who are interested.  Sorry for the mess,
but you should get the idea.

John

==============================================================

? FIELDCHG("JOHNFIX.DBF",{{"CDB_CR","DB_CR"},{"CFTRSDES","FTRSDES"},;
   {"CFTRSREF","FTRSREF"},{"CFBATCH","FBATCH"},;
   {"NFTRSAMT","FTRSAMT"}}),"Names Changed"

***********************************************************************
* Function FieldChg( cDbfFile, aFieldChanges )
*     cDbfFile = Database file to make the changes in
*     aFieldChanges = Each array element is an array of the old
*                     fieldname then the new fieldname
*       Example:  AADD(aFieldChanges,{"oldfieldname","newfieldname"})
*
*     Notes: Can easily be changed to modify anything else, but I would
*            NOT do that.
***********************************************************************
FUNCTION FIELDCHG(cFile,aFields)
LOCAL nSel := SELECT(), aStruct, nFcount, i, nPos, fieldname, newfield
LOCAL nCnt := 0, dbfhandle
   USE (cFile) NEW
   *aStruct := dbStruct()
   nFcount := FCOUNT()
   dbCloseArea()
   SELECT (nSel)
   dbfhandle := fopen(cFile, 2)
   for i := 1 to nFcount
      fseek(dbfhandle, (i*32), 0)
      fieldname := freadstr(dbfhandle,10)
      fieldname := upper(trim(fieldname))
      nPos := ASCAN(aFields,{|x| UPPER(x[1]) == fieldname})
      IF nPos > 0
         newfield := UPPER(aFields[nPos,2])
         fseek(dbfhandle, (i*32), 0)
         fwrite(dbfhandle, padr(newfield,10,chr(0)))
         *fseek(dbfhandle, (i*32)+11, 0)
         *fwrite(dbfhandle, ar[i,2], 1)
         *fseek(dbfhandle, (i*32)+16, 0)
         *fwrite(dbfhandle, chr(ar[i,3])+chr(ar[i,4]), 2)
         nCnt++
      endif
   next
   fclose(dbfhandle)
RETURN(nCnt)



Mon, 21 Jul 2003 23:07:20 GMT  
 Changing Field Names in Database - Looking 4 code

Quote:
> Does anyone have any tips or code that I can use to change the name of
> fields in a database?

Directly modifying the header is the usual method. I'd be surprised to find
that The Oasis <URL:http://www.the-oasis.net/> doesn't hold some code for
doing this.

Quote:
> I have a 3rd party app that exports to a database. To be able to append
> the .DBF output into my program, I need to change 3 of the field names.

Why do you need to change the field names? How are you actually doing the
append? Using the APPEND command? If you're going to go to the trouble of
changing field names and the like why not simply do a "hand copy" of the
data, eg (error checking omitted):

,----
| Use ImportMe New Exclusive
|
| Do While !ImportMe->( eof() )
|
|    IntoMe->( dbappend() )
|
|    IntoMe->Field1 := ImportMe->Foo1
|    IntoMe->Field2 := ImportMe->Foo2
|    IntoMe->Field3 := ImportMe->Foo3
|    IntoMe->Field4 := ImportMe->Foo4
|
|    ImportMe->( dbskip() )
|
| EndDo
`----

Quote:
> I know I could always write and SDF export, and import into a database
> with the desired field names, but I was wondering if anyone has code that
> directly and quickly changes fields names via low level file routines.

Ahh, oh, so you *do* know that technique <g>. Have you checked The Oasis?

--
Dave Pearson                        |   w3ng - The WWW Norton Guide reader.
http://www.davep.org/               |     eg - Norton Guide reader for Linux.
http://www.davep.org/clipper/       |    weg - Norton Guide reader for Windows.
http://www.davep.org/norton-guides/ | dgscan - DGROUP scanner for Clipper.



Mon, 21 Jul 2003 22:35:15 GMT  
 Changing Field Names in Database - Looking 4 code
I wrote one long ago and posted on OASIS, Just query there !

Regards
Nitin


Quote:
> Does anyone have any tips or code that I can use to change the name of
> fields in a database?

> I have a 3rd party app that exports to a database.  To be able to append
the
> .DBF output into my program, I need to change 3 of the field names.

> I know I could always write and SDF export, and import into a database
with
> the desired field names, but I was wondering if anyone has code that
> directly and quickly changes fields names via low level file routines.

> John



Thu, 24 Jul 2003 19:09:14 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Database Field Names (!!!)

2. Database-Field-Names

3. Change field name (re posted from the french NG)

4. need to change field name every month

5. Modify Structure and Change Name of fields

6. Code to Modify/Delete Fields From a Database

7. comparing flat file fields to database fields

8. SQLSelect - Loading database/table field data into an Edit field

9. Looking for C++ code to access Clipper database files

10. Looking for AWK (or whatever) code to figure out genders of names

11. Field names in a field...

12. Selecting Fields Based On Field Name

 

 
Powered by phpBB® Forum Software