B4J Question LoadCSV2 - comparing headers

h725

Active Member
Licensed User
Longtime User
Hello Community,

I am a little bit stuck in the following task:

In Visual Basic, I load a csv-file into a datatable.
When I import the datatable into my database, I am using
a loop where I check the headername:

B4X:
For i = 0 to datatable.rows.count - 1
Dim name as String = datatable.Rows(i).Item("NAME").ToString)
Next

In B4J I can load a csv file including it's headers with LoadCSV2.
Is there a way to make a similar header comparison like in Visual Basic?

Kind regards
h725
 

emexes

Expert
Licensed User
Backup Plan might be to make a Map ColumnNameToNumber using Headers, eg:

B4X:
Dim ColumnNameToNumber As Map
ColumnNameToNumber.Initialize
For C = 0 To Headers.Size - 1
    ColumnNameToNumber.Put(Headers.Get(C), C)    'to translate column header name to column number
Next

and then:

B4X:
Dim name as String = datatable.Get(i).As(List).Get(ColumnNameToNumber.Get("NAME").As(Int))

or in step-by-step form:
B4X:
Dim TempColumnNumber As Int = ColumnNameToNumber.Get("NAME")
Dim TempRow As List = datatable.Get(i)
Dim name As String = TempRow.Get(TempColumnNumber)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Or maybe
B4X:
    lis = su.LoadCSV2("c:/temp","test.cvs",",",headers)

    For Each item() As Object In lis
        Dim name As String = item(headers.IndexOf("NAME"))
    Next
 
Upvote 0

h725

Active Member
Licensed User
Longtime User
Thank you for the input.

I used the step-by-step solution which is working fine.
 
Last edited:
Upvote 0
Top