Fieldname limit to 10 char. giving problem

Fieldname limit to 10 char. giving problem

Postby Marc Venken » Sun Oct 23, 2016 1:11 pm

Hello,

I convert csv files to dbf files by code.

I have the fieldnames in a array adata from a other piece of code, and want to make a dbf out of it.
The problem is that the fieldnames in csv are often longer than 10 characters.

adata contains example :

ID
Name
Price
information1
information2
information3
...

The routine below will do

ID
Name
Price
informatio
informatio
informatio

I need something like (2 digits to be save, so 8 from the fieldname and 2 incremental)

informat01
informat02
informat03

I'm working on Ascan and so, but so far no luck...

Code: Select all  Expand view

function Builddbf(adata,Alengte,cDbf)
   LOCAL aStru := {}

//   msginfo(atostr(adata))

   for i = 1 to len(adata)
     cField = STRTRAN(aData[i], '"', '')
     aAdd( aStru, { cField , "C", alengte[i]+1 , 0 } )
   next
   dbCreate( cDbf , aStru )

return

 
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1425
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Fieldname limit to 10 char. giving problem

Postby Rick Lipkin » Sun Oct 23, 2016 4:25 pm

Marc

Question .. do you know the structure of the CSV file ? If so, you may be able to use the SDF append from option ..

Code: Select all  Expand view

..  create your temp database.. the .dbf fields must be in the same order as the .csv

Select 1
Append from yourfile.csv SDF delimited by ","
 


Not very elegant but it should work or you can use Ole and Ado to take an .Xls(x) to .Dbf .. lots of examples in this forum on how to import Excel.

Rick Lipkin
User avatar
Rick Lipkin
 
Posts: 2663
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Fieldname limit to 10 char. giving problem

Postby Marc Venken » Sun Oct 23, 2016 6:46 pm

Hey,

The append from is no option.

I read the csv files and change some values while importing. The csv's are never the same as the master data and there are many csv files to process trough time.

I only know some what of DBF's, so other struff like sql, .... are no option on this moment. (time limit) in order to learn the rest.
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1425
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Fieldname limit to 10 char. giving problem

Postby James Bott » Sun Oct 23, 2016 10:36 pm

Marc,

This is one reason why I suggested changing the header of the CSV file before it is imported into a DBF. If you change the header to use the standard fieldnames in the master DBF, then importing and processing it will be much easier.

Another possibility is to just read in the lines of the CSV file one at a time, then move the data to an array then process the array.

There are other possibilities too.

James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Fieldname limit to 10 char. giving problem

Postby AHF » Tue Oct 25, 2016 9:31 am

James,

Is this what you need ?

Code: Select all  Expand view


if len( cField ) > 10
   n++
   cField := substr( cField, 1, 10 - len ( alltrim( str( n, 0 ) ) ) ) + ;
                    padl( alltrim( str( n, 0) ), 2, "0" )
endif

 
Regards
Antonio H Ferreira
AHF
 
Posts: 838
Joined: Fri Feb 10, 2006 12:14 pm

Re: Fieldname limit to 10 char. giving problem

Postby aferra » Tue Oct 25, 2016 3:04 pm

LOCAL cFileCSV := "c:\dircvs\name.cvs"

oTxtFile := TTxtFile():New( cFileCSV, 0 )
oTxtFile:Skip() //skip the first line of the header

while !oTxtFile:lEoF()

?StrToken( oTxtFile:cLine, 1, ";" )
?StrToken( oTxtFile:cLine, 2, ";" )
?StrToken( oTxtFile:cLine, <n>, ";" )

oTxtFile:Skip()

ENDDO

oTxtFile:Close()

that's how I read the .csv files or any other
aferra
 
Posts: 88
Joined: Wed Apr 28, 2010 6:34 pm

Re: Fieldname limit to 10 char. giving problem

Postby Marc Venken » Tue Oct 25, 2016 5:54 pm

AHF wrote:
Code: Select all  Expand view


if len( cField ) > 10
   n++
   cField := substr( cField, 1, 10 - len ( alltrim( str( n, 0 ) ) ) ) + ;
                    padl( alltrim( str( n, 0) ), 2, "0" )
endif

 


Hey,

This was exactly what I needed. Thanks.

Marc
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1425
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Fieldname limit to 10 char. giving problem

Postby Marc Venken » Tue Oct 25, 2016 6:15 pm

aferra wrote:LOCAL cFileCSV := "c:\dircvs\name.cvs"

oTxtFile := TTxtFile():New( cFileCSV, 0 )
oTxtFile:Skip() //skip the first line of the header

while !oTxtFile:lEoF()

?StrToken( oTxtFile:cLine, 1, ";" )
?StrToken( oTxtFile:cLine, 2, ";" )
?StrToken( oTxtFile:cLine, <n>, ";" )

oTxtFile:Skip()

ENDDO

oTxtFile:Close()

that's how I read the .csv files or any other


This is interesting, and so much clearer and easy !!
A total other way as I do, but very nice.

Can you point me to an information page where this is more explaned ? I would like to look into this more.

Marc
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1425
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 63 guests