Where is the code for importing a pipe separated file into a table?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
mustafa
Posts: 1172
Joined: Fri Mar 20, 2009 11:38 am
DBs Used: DBF
Location: Alicante - Spain
Contact:

Re: Where is the code for importing a pipe separated file into a table?

Post by mustafa »

Franco friend:
Let me disagree with your solution.
if we apply to a txt file with Pipe separators
with your solution : ---> "Sample1.prg"

APPEND FROM FILE.TXT DELIMITED WITH CHR(124)

It only transfers me to the Franchis2.dbf the first
column FROM DATABASE -> "R_SOCIAL"

In my solution "Sample2.prg" the
entire file Franchis.dbf to Franchis2.dbf
via "Sample2.txt" with PIPE "|" Ok!

It is also working with :
APPEND FROM sample2.txt DELIMITED WITH ({, CHR(124) })

Regards/Salam/Saludos

Mustafa
Attachments
PIPE.ZIP
(1.41 KiB) Downloaded 282 times
HGAutomator
Posts: 197
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: Where is the code for importing a pipe separated file into a table?

Post by HGAutomator »

Hi mustafa,

Code: Select all

APPEND FROM sample2.txt DELIMITED WITH ({, CHR(124) })
did indeed work.

Unfortunately, it didn't seem to pull in any records, if there was a field condition like "CATEGORY='DETAIL'". That isn't serious, because I can work around it.

However, more seriously is that the Harbour version doesn't correctly interpret dates like VFP does. So a date like '06/15/2023'' is pulled in as a blank.
User avatar
mustafa
Posts: 1172
Joined: Fri Mar 20, 2009 11:38 am
DBs Used: DBF
Location: Alicante - Spain
Contact:

Re: Where is the code for importing a pipe separated file into a table?

Post by mustafa »

after
#include <hmg.ch>
Function Main
put this:

Code: Select all

SET CODEPAGE TO UNICODE
SET CENTURY ON
SET DATE FRENCH
SET DATE FORMAT TO 'dd/mm/yyyy'
See if it works?
edk
Posts: 999
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Where is the code for importing a pipe separated file into a table?

Post by edk »

mustafa wrote: Sun Jun 18, 2023 10:05 am after
#include <hmg.ch>
Function Main
put this:

Code: Select all

SET CODEPAGE TO UNICODE
SET CENTURY ON
SET DATE FRENCH
SET DATE FORMAT TO 'dd/mm/yyyy'
See if it works?
Pay attention to the indicated date '06/15/2023'. It is in the American format 'mm/dd/yyyy' and not French (there is no month 15 in the calendar :mrgreen: )

@HGAutomator. Can U upload sample CSV file with a pipe delimiter :?:
franco
Posts: 877
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: Where is the code for importing a pipe separated file into a table?

Post by franco »

When Mustafa uses COPY TO sample2.txt DELIMITED WITH ({,"|"}) it puts double quoted around fields.
So must use APPEND FROM sample2.txt DELIMITED WITH ({,"|"}) to append properly.
If you use
COPY TO sample2.txt DELIMITED WITH chr(124)
or COPY TO sample2.txt DELIMITED WITH "|"
It delimites with |
You can then
APPEND FROM sample2.txt DELIMITED WITH chr(124)
or
APPEND FROM sample2.txt DELIMITED WITH "|"
All The Best,
Franco
Canada
User avatar
mustafa
Posts: 1172
Joined: Fri Mar 20, 2009 11:38 am
DBs Used: DBF
Location: Alicante - Spain
Contact:

Re: Where is the code for importing a pipe separated file into a table?

Post by mustafa »

Franco friend:
Unfortunately your solution is giving me "ERROR"
Look at the sample "Sample1.prg" which creates "sample1.txt"
with Double " and PIPE

Solution in ---> Franxy1.dbf <--- OK!

The sample "Sample2.prg" according to your instructions does not create
Double " , but incorporates "," and no PIPE
The resulting file is missing data, it is mixed up.

Result in ---> Franxy2.dbf , missing the first
registration in R_SOCIAL and ADDRESS

PS: for edk, I didn't notice that the date is American
I don't speak English I'm sorry
Attachments
PIPE2.ZIP
(2.71 KiB) Downloaded 255 times
franco
Posts: 877
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: Where is the code for importing a pipe separated file into a table?

Post by franco »

Mustafa I find you must append with the same item you delimit with.

If you use
or COPY TO sample1.txt DELIMITED WITH "|"
It delimites with |
You can then
APPEND FROM sample1.txt DELIMITED WITH "|"

For some unknown reason ypu can not use capitals on chr in the program
COPY TO sample1.txt DELIMITED WITH CHR(124)
It delimites with C and will not work something in hmg

If you use
COPY TO sample1.txt DELIMITED WITH chr(124) // small chr
It delimites with c // small c seems to be pipe in ascii codes ?
You can then
APPEND FROM sample1.txt DELIMITED WITH chr(124)

It took a bit to find capitals on chr do not work.
All The Best,
Franco
Canada
User avatar
mustafa
Posts: 1172
Joined: Fri Mar 20, 2009 11:38 am
DBs Used: DBF
Location: Alicante - Spain
Contact:

Re: Where is the code for importing a pipe separated file into a table?

Post by mustafa »

By default HMG works in unicode, for this you have to use:
hb_ucode()
hb_uchar()
Instead of asc/chr which works only with Ansi code pages

Save PRG code, UTF-8 encoded
SET CODEPAGE TO UNICODE
and change chr(124) to HB_UCHAR(124) , but it still goes wrong <---ERROR ! :(

It is only working with:
COPY TO sample1.txt DELIMITED WITH ({ , HB_UCHAR(124 ) })
APPEND FROM sample1.txt DELIMITED WITH ({, HB_UCHAR(124) })
Last edited by mustafa on Wed Jun 21, 2023 11:43 am, edited 1 time in total.
franco
Posts: 877
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: Where is the code for importing a pipe separated file into a table?

Post by franco »

***** You can try all of the following code I all works for me except capital CHR
***** You will need Franxy1.dbf in folder. This program name is Sample1.prg

Code: Select all

#include "hmg.ch"

FUNCTION MAIN()

SET CODEPAGE TO UNICODE
SET CENTURY ON

    USE Franxy NEW                                 
    COPY TO Franxy1                           
    DBCloseAll()
*********************** This Works for me with quotes ****************
    USE Franxy1  NEW                                 
    COPY TO sample1.txt DELIMITED WITH ({, "|" }) 
    DBCloseAll()

    USE Franxy1  New 
    ZAP 
    APPEND FROM sample1.txt DELIMITED WITH ({, chr(124)}) 
**********************************************************************

*********************** This Works for me without quotes ****************
*    USE Franxy1  NEW                                 
 *   COPY TO sample1.txt DELIMITED WITH "|" 
  *  DBCloseAll()
*
 *   USE Franxy1  New 
  *  ZAP 
   * APPEND FROM sample1.txt DELIMITED WITH "|" 
**********************************************************************

*********************** This Works for me without quotes  show small c as delimiter in text file but works ****************
 *   USE Franxy1  NEW                                 
  *  COPY TO sample1.txt DELIMITED WITH chr(124) 
   * DBCloseAll()
*
 *   USE Franxy1  New 
  *  ZAP 
   * APPEND FROM sample1.txt DELIMITED WITH chr(124) 
**********************************************************************

******** This Does Not Work for me with Capial CHR  show Capital C as delimiter Missing First two Fields****************
*    USE Franxy1  NEW                                 
 *   COPY TO sample1.txt DELIMITED WITH CHR(124) 
  *  DBCloseAll()
*
 *   USE Franxy1  New 
  *  ZAP 
   * APPEND FROM sample1.txt DELIMITED WITH CHR(124)
**********************************************************************

    GO TOP

	DEFINE WINDOW Form_1 ;
		AT 0,0 ;
		WIDTH 1000 ;
		HEIGHT 550 ;
		TITLE 'Test' ;
		MAIN ;
                BACKCOLOR (200,200,200) 


		DEFINE BROWSE BR1
			ROW 10
			COL 10
			WIDTH 1000
			HEIGHT 400
			HEADERS {'R_social','Direccion','Telefono', 'Ciudad', 'Pais', 'Name', 'Date'}
			FIELDS  {'R_social','Direccion','Telefono', 'Ciudad', 'Pais', 'Name', 'Date' }
			WIDTHS {140,140,140,140,140,140,100}
			WORKAREA franxy1
			BACKCOLOR {180,180,180}

		END BROWSE
		

		
	END WINDOW

	CENTER WINDOW Form_1

	ACTIVATE WINDOW Form_1

Return
***************** I use to compile. and have Franxy.dbf in folder
*@ECHO OFF
*call c:\hmg.3.4.4\build.bat sample1 %*
All The Best,
Franco
Canada
Red2
Posts: 281
Joined: Sat May 18, 2019 2:11 pm
DBs Used: Visual FoxPro, FoxPro
Location: United States of America

Re: Where is the code for importing a pipe separated file into a table?

Post by Red2 »

Thank you Franco,

The command syntax you suggested,
APPEND FROM "....txt" DELIMITED WITH ({, chr(124)}),
works for me and is the "delimited character" hint I wanted.

Thanks again,
Red2
Post Reply