Subject Re: format for .CSV file
From Mervyn Bick <invalid@invalid.invald>
Date Wed, 12 Jun 2019 17:49:02 +0200
Newsgroups dbase.getting-started
Attachment(s) make_special_csv.prg

On 2019-06-12 3:33 PM, Matt Milner wrote:
> I use dBase Plus 12.
> I am writing an interface between my software and that of another company.
> They have asked for data to be transferred in .CSV format with the first line containing the list of field names in comma delimited format.  They have also stipulated that empty fields be represented by double quotes and numeric fields have quotes around the numbers.  Is there a simple way for me to comply?
>

"Simple" is relative. :-)  On the other hand it's not difficult (which
is also relative) either.

usvout.prg in the dUFLP can do some of the requirements but not all of
them.  It is definitely not standard practice to wrap numeric values in
quotes.  It is also not usual to use delimiters for empty fields.  To
meet the customer's requirements I'm afraid you are going to go the DIY
route.

A very "quick and dirty" example is attached.  If the data contains any
dates you will need to add code to present them the way the client wants.

Mervyn



q = new query()
q.sql = 'select * from whatever'
q.active = true
f = new file()
f.create('special_csv.csv')
cLine = ''
for n = 1 to q.rowset.fields.size
   cLine += '"'+q.rowset.fields[n].fieldname+'",'
next
cLine = substr(cLine,1,len(cLine)-1) //drop last comma
f.puts(cLine)
do while not q.rowset.endofset
  cLine = ''
  for n = 1 to q.rowset.fields.size
     if q.rowset.fields[n].type = 'N'
       cLine += "'"+iif(empty(q.rowset.fields[n].value),0,q.rowset.fields[n].value)+"',"
     elseif  q.rowset.fields[n].type = 'C' or q.rowset.fields[n].type = 'M'
       cLine += '"'+iif(empty(q.rowset.fields[n].value),space(1),trim(q.rowset.fields[n].value))+'",'
     else
       //deal with other field types
     endif
   next
   cLine = substr(cLine,1,len(cLine)-1) //drop last comma
   f.puts(cLine)
   q.rowset.next()  
enddo    
f.close()
q.active = false