Board index » delphi » Can I import a .CSV-file with TBatchMove?

Can I import a .CSV-file with TBatchMove?

I'm trying to import a comma-separated valuefile into
a table. Obviously I'm doing something wrong.  But
my mind of thinking won't leave the path it's on...

In the Source-property of my TBatchMove-component
I wrote the filename of my .CSV-file, but got an error
message. "Bad property value". The rest of the
properties seems clear to me (I think).

I'd appreciate any help!

bK

 

Re:Can I import a .CSV-file with TBatchMove?


I finally made it! No problem at all. Two cups of coffee
and I was able to solve this little problem of mine.

If anyone are interested I will mail the simple solution.

bK

Quote
Bj?rn Kristiansen wrote in message <6a8ifm$oh...@o.online.no>...
>I'm trying to import a comma-separated valuefile into
>a table. Obviously I'm doing something wrong.  But
>my mind of thinking won't leave the path it's on...

>In the Source-property of my TBatchMove-component
>I wrote the filename of my .CSV-file, but got an error
>message. "Bad property value". The rest of the
>properties seems clear to me (I think).

>I'd appreciate any help!

>bK

Re:Can I import a .CSV-file with TBatchMove?


I got a lot of responses on my posting, so why not post
the solution? OK, her we go.

To make sure everyone understands, I will try to describe it
as simple as I can. And I will do it with an example.

We've just made a paradox table called countrycodes for
storing and retreiveing country-codes (GB: Great Britain,
N: Norway, ...). It consists of only two fields:

    FIELD NAME    TYPE    SIZE
    CountryCode        A            3
    Country                  A         20

From a spreadsheet (or whatever) we export the countrycodes to
a .csv-file named countrycodes.csv. We then rename the file to
countrycodes.txt, since Delphi wants it that way.

Now we have to make a schema-file that we call countrycodes.sch.
and it will look like this:

    [CountryCodes]
    FILETYPE=VARYING
    CHARSET=ASCII
    DELIMITER="
    SEPARATOR=;
    FIELD1=CountryCode,char,3,0,0
    FIELD2=Country,char,20,0,0

Since the two fields occupy a variable amount of space we use VARYING
as value for item FILETYPE (GB, N and Great Britain, Norway use variable
amount of space for their respective fields).
Since the .csv-file we renamed is nothing more than a text-file we set
CHARSET to value ASCII.
The DELIMITIER item we set to ", and the SEPARATOR item to ;
If for some reason the fields in countrycodes.txt is separated with a comma,
set the value for the SEPARATOR item to comma.
The FIELDx item (x being a number from 1 to the maximum number of fields)
has a multiple parameter as a value. The first parameter specifies the
field-
name in the paradox-table (I think..), the second specifies what kind of
data
we are importing, and the third it's size. The fourth and fifth parameter we
set
to zero. We only use the fourth parameter when importing numbers with
decimals and, and the fifth to specify offset when the value of FILETYPE
item
is set to FIXED.

We now have two files called countrycodes. Only the extension is different,
because Delphi wants it that way.

Now start Delphi, with a new project, and add the following components to
the form:

    TTable called SourceTable
    TTable called DestinationTable
    BatchMove called BatchMove1
    DataSource called DataSource1
    DBGrid called DBGrid1

1) Set SourceTable.TableType to ttASCII
2) Set SourceTable.DatabaseName to the dir or alias of your csv-file
3) Set SourceTable.TableName to countrycodes.txt
4) Set DestinationTable.TableType to ttParadox
5) Set DestinationTable.DatabaseName to the dir or alias of your
paradox-table
6) Set DestinationTable.TableName to the tablename for the paradox-table
7) Set BatchMove1.Source to SourceTable
8) Set BatchMove1.Destination to DestinationTable

Before we start importing we want to check wether or not the process will
succeed.

9) Set DBGrid1.DataSource to DataSource1
10) Set DataSource1.Dataset to SourceTable
11) Set SourceTable.Active to TRUE

The DBGrid1 are now displaying the textfile. If it looks alright then
rightclick the
BatchMove1 component and select Execute.

You have now imported a text-file to a paradox-table.

Bj?rn

Other Threads