Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import comma delimited text file into datawindow (powerbuilder 11.5)

Hi good day I'm very new to powerbuilder and I'm using PB 11.5

Can someone know how to import comma delimited text file into datawindow.

Example Text file

"1234","20141011","Juan, Delacruz","Usa","001992345456"...

"12345","20141011","Arc, Ino","Newyork","005765753256"...

How can I import the third column which is the full name and the last column which is the account number. I want to transfer the name and account number into my external data window. I've tried to use the ImportString(all the rows are being transferred in one column only). I have three fields in my external data window.the Name and Account number.

Here's the code

  ls_File = dw_2.Object.file_name[1]
 li_FileHandle = FileOpen(ls_File)
 li_FileRead = FileRead(li_FileHandle, ls_Text) 
 DO WHILE li_FileRead > 0
 li_Count ++
 li_FileRead = FileRead(li_FileHandle, ls_Text)

 ll_row = dw_1.ImportString(ls_Text,1)

Loop.

Please help me with the code! Thank You

like image 899
Jeyse Bel Avatar asked Nov 21 '22 15:11

Jeyse Bel


1 Answers

It seems that PB expects by default a tab-separated csv file (while the 'c' from 'csv' stands for 'coma'...).

Add the csv! enumerated value in the arguments of ImportString() and it should fix the point (it does in my test box).

Also, the columns defined in your dataobject must match the columns in the csv file (at least for the the first columns your are interested in). If there are mode columns in the csv file, they will be ignored. But if you want to get the 1st (or 2nd) and 3rd columns, you need to define the first 3 columns. You can always hide the #1 or #2 if you do not need it.

BTW, your code has some issues :

  1. you should always test the return values of function calls like FileOpen() for stopping processing in case of non-existent / non-readable file
  2. You are reading the text file twice for the first row: once before the while and another inside of the loop. Or maybe it is intended to ignore a first line with column headers ?

FWIF, here is a working code based on yours:

string ls_file = "c:\dev\powerbuilder\experiment\data.csv"
string ls_text
int li_FileHandle, li_fileread, li_count
long ll_row

li_FileHandle = FileOpen(ls_File)
if li_FileHandle < 1 then
    return
end if

li_FileRead = FileRead(li_FileHandle, ls_Text) 
DO WHILE li_FileRead > 0
    li_Count ++
    ll_row = dw_1.ImportString(csv!,ls_Text,1)
    li_FileRead = FileRead(li_FileHandle, ls_Text)//read next line
Loop
fileclose(li_fileHandle)
like image 116
Seki Avatar answered May 16 '23 08:05

Seki