| Subject |
Re: importing csv file |
| From |
Mervyn Bick <invalid@invalid.invalid> |
| Date |
Wed, 30 Jun 2021 10:37:18 +0200 |
| Newsgroups |
dbase.getting-started |
On 2021/06/29 19:33, Mustansir Ghor wrote:
> Dear Mervyn Sir
>
> I read your below remark on date but could not figured the code to add. Will you help me on this.
>
> That code will work with most .csv files. If, however, you have to
> import dates you may need to add code to format the date before it gets
> saved to the .dbf file.
>
I've added some comments to a snippet of the code I posted before.
......
do while not f.eof()
cRead = f.gets(100000) //Fetch a line from the .csv file.
aRecord = new stringex(cRead).breakstring(";",true)
//check separator in .csv file. It is normally a comma but,
//because I use a comma as a decimal point, Excel made the .csv
//file with ; as the separator. Change the ";" to "," if your
//.csv uses a comma as the separator.
//
//stringex() creates an array with each field from the .csv in
//an element. The array is the same size as the rowset's fields array.
//The following code places the contents of each element in the array
//created by stringex() into the corresponding field of the table.
q.rowset.beginAppend()
for n = 1 to aRecord.size
q.rowset.fields[n].value = aRecord[n]
next
q.rowset.save()
enddo
......
Where the fields in the table are numeric or character then the data in
each element of the array created by stringex() can be placed directly
into each field in the fields array.
Where the field type in the table is DATE or DATETIME the string in the
array created by stringex() needs to be converted to a DATE or a
DATETIME type first. The actual conversion depends on the format of the
string in the .csv file. This is why Gaetano has asked for a sample.
It is extremely difficult to write a universal program to save data from
a .csv file to a table. It is, however, very easy to write a program to
save data from a specific .csv file to a specific table using stringex()
from the dUFLP. The .csv file you were using previously only had three
character fields. There was, therefore, no need to deal with anything
else. If you have fields other than numeric or character to deal with
it is easy enough to test for them and then convert the string for the
.csv file to the appropriate type.
q.rowset.beginAppend()
for n = 1 to aRecord.size
if q.rowset.fields[n].type = 'DATETIME'
//code to convert string to dtDATETIME
q.rowset.fields[n].value = dtDATETIME
elseif q.rowset.fields[n].type = 'DATE'
//code to convert string to dDATE
q.rowset.fields[n].value = dDATE
elseif q.rowset.fields[n].type = 'TIME'
//code to convert string to tTIME
q.rowset.fields[n].value = tTIME
else //all numeric and character field types can simply be saved
q.rowset.fields[n].value = aRecord[n]
endif
next
q.rowset.save()
enddo
Mervyn.
|
|