Android Question [SOLVED] Export Specific B4XTable Columns

ProjectGroup19

Active Member
Licensed User
Greetings community,

Below is the code I use to export my data from my B4xTable to csv and it works for me. But now, i want to be able to export specific columns. Lets say, i want to export all the columns ["USERNAME", "ID","FULLNAME","DATE OF BIRTH","CONTACT","EMAIL","USERTYPE","INSTITUTION","LASTLOGIN"] except that of the ["ID","LASTLOGIN","DATE OF BIRTH"]. How do i do that?


B4X:
Private const CSVFileName As String ="userDetails.csv"
    Dim data As List
    data.Initialize

    Dim headers As List
    headers.Initialize
    headers.AddAll(Array As String("USERNAME", "ID","FULLNAME","DATE OF BIRTH","CONTACT","EMAIL","USERTYPE","INSTITUTION","LASTLOGIN"))

    Dim rs As ResultSet = B4XTable1.sql1.ExecQuery($"SELECT * FROM data"$)

    If  rs.RowCount>0 Then
        Dim sf As Object = xui.Msgbox2Async($"Do you want to export this table?"$, "Export", "Yes", "", "No", Null)
        Wait For (sf) Msgbox_Result (Result As Int)

        If Result = xui.DialogResponse_Positive Then
            Do While rs.NextRow

                Dim row(B4XTable1.Columns.Size) As String

                For i = 0 To B4XTable1.Columns.Size - 1

                    Dim c As B4XTableColumn = B4XTable1.Columns.Get(i)

                    row(i) = rs.GetString(c.SQLID)

                Next

                data.Add(row)

            Loop

            rs.Close

            Dim su As StringUtils

            rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)

            Wait For activity_permissionresult(permission As String,res As Boolean)



            If res=True Then
                su.SaveCSV2(File.Combine(File.DirRootExternal,"Download"), CSVFileName, ",", data, headers)
            
                Dim Answ As Object
                Dim Txt As String
                Txt = $"List successfully exported."$
                Answ = Msgbox2Async(Txt, "Export", "Yes", "", "No",  Null,False)
                Wait For (Answ) msgbox_result(resul As Int)
                If resul = DialogResponse.POSITIVE Then
                    ToastMessageShow($"You can find the exported list in your downloads folder as "${CSVFileName}""$,True)
            
                           
                Else
                    ToastMessageShow($"You can find the exported list in your downloads folder as "${CSVFileName}""$,True)
            
                    Return
                End If
            
            Else
                ToastMessageShow("Storage permission not granted",True)
                Return
            End If
        
        End If


    Else
        MsgboxAsync("Sorry!! You cannot export an empty table.","Export")
        ToastMessageShow("There is no data to export.", True)
    End If
 
Solution
It should be
B4X:
Dim InterestingColumns As List = Array("USERNAME", "FULLNAME", "CONTACT","EMAIL","USERTYPE","INSTITUTION")
    Do While rs.NextRow
                Dim row(InterestingColumns.Size) As String
               Dim i As Int = 0
               For Each c As B4XTableColumn In B4XTable1.Columns
                    If InterestingColumns.IndexOf(c.Title) = -1 'make sure that the cases are correct
                         Continue
                    End If
                    row(i) = rs.GetString(c.SQLID)
                    i = i + 1
                Next

Erel

B4X founder
Staff member
Licensed User
Longtime User
The simplest way to do it is with:

B4X:
Dim InterestingColumns As List = Array("USERNAME", "FULLNAME", "CONTACT","EMAIL","USERTYPE","INSTITUTION")
    Do While rs.NextRow

                Dim row(B4XTable1.Columns.Size) As String

                For i = 0 To B4XTable1.Columns.Size - 1

                    Dim c As B4XTableColumn = B4XTable1.Columns.Get(i)
                    If InterestingColumns.IndexOf(c.Title) = -1 'make sure that the cases are correct
                         Continue
                    End If
                    row(i) = rs.GetString(c.SQLID)

                Next
 
Upvote 0

ProjectGroup19

Active Member
Licensed User
The simplest way to do it is with:

B4X:
Dim InterestingColumns As List = Array("USERNAME", "FULLNAME", "CONTACT","EMAIL","USERTYPE","INSTITUTION")
    Do While rs.NextRow

                Dim row(B4XTable1.Columns.Size) As String

                For i = 0 To B4XTable1.Columns.Size - 1

                    Dim c As B4XTableColumn = B4XTable1.Columns.Get(i)
                    If InterestingColumns.IndexOf(c.Title) = -1 'make sure that the cases are correct
                         Continue
                    End If
                    row(i) = rs.GetString(c.SQLID)

                Next
This worked great ☺️. Thank you @Erel.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
This worked great
I tried the method proposed to you by Erel, but the text file that was exported showed all fields. The fields that were skipped had empty strings. For instance, if you have 3 columns in your table but one field was not included in the text file, the result looked like this:
117,,greenland
118,,detroit
119,,reno
120,,pretoria
121,,prague
How were you able to not have gaps in the text file. Maybe, you can post a couple of lines showing what your text file looks like. You can change the data in it if confidential.
Thank you
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It should be
B4X:
Dim InterestingColumns As List = Array("USERNAME", "FULLNAME", "CONTACT","EMAIL","USERTYPE","INSTITUTION")
    Do While rs.NextRow
                Dim row(InterestingColumns.Size) As String
               Dim i As Int = 0
               For Each c As B4XTableColumn In B4XTable1.Columns
                    If InterestingColumns.IndexOf(c.Title) = -1 'make sure that the cases are correct
                         Continue
                    End If
                    row(i) = rs.GetString(c.SQLID)
                    i = i + 1
                Next
 
Upvote 1
Solution

ProjectGroup19

Active Member
Licensed User
It should be
B4X:
Dim InterestingColumns As List = Array("USERNAME", "FULLNAME", "CONTACT","EMAIL","USERTYPE","INSTITUTION")
    Do While rs.NextRow
                Dim row(InterestingColumns.Size) As String
               Dim i As Int = 0
               For Each c As B4XTableColumn In B4XTable1.Columns
                    If InterestingColumns.IndexOf(c.Title) = -1 'make sure that the cases are correct
                         Continue
                    End If
                    row(i) = rs.GetString(c.SQLID)
                    i = i + 1
                Next
This worked better.. Thanks again @Erel and @Mahares
 
Upvote 0
Top