Android Question How B4XTable data to csv?

warayTek

Member
Licensed User
Hello, is there a way to export data to csv from list with B4XTable?
B4XTable1.SetData(myList)
The only solution I'm thinking of is to save it first to sqlite.
 

Mariano Ismael Castro

Active Member
Licensed User
Hello, is there a way to export data to csv from list with B4XTable?
B4XTable1.SetData(myList)
The only solution I'm thinking of is to save it first to sqlite.
 
Upvote 1

warayTek

Member
Licensed User
I have tried it but I was stuck. But I will try to dig deeper into it.
Thank you.
 
Upvote 0

warayTek

Member
Licensed User
You can create a small project where you show the error and upload it to help you
I will explain how my program is supposed to work.
The code below fetches data from Mysql and places it into the list.
B4X:
Sub getSaleList
    Try
    '
    Dim j As HttpJob
    j.Initialize("", Me)
    Log("code..")
    Dim vstartDate, vendDate As String
    vstartDate = B4XPages.MainPage.mmenupage.datestart
    vendDate = B4XPages.MainPage.mmenupage.dateend
    Log(vstartDate & "  " & vendDate)
    j.Download2(B4XPages.MainPage.gLink,    Array As String("action","Sales_List","sdate",vstartDate,"edate",vendDate))
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim strdata As String
        strdata = j.GetString
        Log(strdata)
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
                        
        saleList = jp.NextArray
        If saleList.Size < 1 Then
            MsgboxAsync("Records not found! " & Chr(10) & "Date: " & vstartDate & " - " & vendDate,"Admin")
            Return
        End If
        ProgressDialogShow2("Please wait loading...",True)
        Dim myList As List
        myList.Initialize
        myList.clear
        For Each mydata As Map In saleList
            Dim row(6) As Object
                
            row(0) = mydata.Get("transaction_no")
            row(1) = mydata.Get("category")
            row(2) = mydata.Get("drso")
            row(3) = mydata.Get("description")
            row(4) = mydata.Get("issue_date")
            row(5) = mydata.Get("total_ammount")
            
                
            myList.Add(row)
            'myList.Add(mydata.Get("branch_no") & "," & mydata.Get("branch_name") & "," & mydata.Get("branch_address") & "," & mydata.Get("branch_contact_no"))
        Next
            
        Log(mydata.Size)
        B4XTable1.AddColumn("TransNo.", B4XTable1.COLUMN_TYPE_TEXT)
        B4XTable1.AddColumn("Category", B4XTable1.COLUMN_TYPE_TEXT)
        B4XTable1.AddColumn("DRSO", B4XTable1.COLUMN_TYPE_TEXT)
        B4XTable1.AddColumn("Description", B4XTable1.COLUMN_TYPE_TEXT)
        B4XTable1.AddColumn("IssueDate", B4XTable1.COLUMN_TYPE_TEXT)
        B4XTable1.AddColumn("Total", B4XTable1.COLUMN_TYPE_TEXT)

        B4XTable1.SetData(myList)
            
    End If
        
    Catch
    'Log("Error")
    MsgboxAsync("Error on connection","System admin")
    ProgressDialogHide
    End Try
    ProgressDialogHide
    B4XTable1.Refresh
    j.Release
End Sub

The next code below is the exporting to csv.
B4X:
Public Sub ExportTableToCSV
    Dim fileName,pma,pmb As String
    DateTime.DateFormat = "yyyy-MM-dd"
    pma = DateTime.Date(DateTime.Now)
    DateTime.TimeFormat = "HH:mm:ss"
    pmb = DateTime.Time(DateTime.Now)
    CSVFileName = "Sales_" &pma & pmb & ".csv"
    headers.Initialize
    Dim data As List
    data.Initialize
    Dim rs As ResultSet = B4XTable1.sql1.ExecQuery("SELECT * FROM data")
    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
    Dim npath As String
    npath = B4XPages.MainPage.AppFolderPath
    su.SaveCSV2(npath, CSVFileName, ",", data, headers)
    Log("Done export!")
End Sub

My trouble is how can I modify the block of code in yellow to match the requirements of the data to be able to do so.

1698107946265.png
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
My trouble is how can I modify the block of code in yellow to match the requirements of the data to be able to do so.
B4X:
headers.Initialize
headers.AddAll(Array( "TransNo", "Category", "DRSO", "Description",  "IssueDate", "Total" ))  'or whatever col names you want
Dim data As List
data.Initialize
Dim rs As ResultSet = B4XTable1.sql1.ExecQuery("SELECT * FROM data")
Do While rs.NextRow
  Dim row(6) As String
  row(0) = rs.GetString("c0")
  row(1) = rs.GetString("c1")
  row(2) = rs.GetString("c2")
  row(3) = rs.GetString("c3")
  row(4) = rs.GetString("c4")
  row(5) = rs.GetString("c5")
  data.Add(row)
Loop
rs.Close
Dim su As StringUtils
'rest of code
 
Last edited:
Upvote 0

warayTek

Member
Licensed User
B4X:
headers.Initialize
headers.AddAll(Array( "TransNo", "Category", "DRSO", "Description",  "IssueDate", "Total" ))  'or whatever col names you want
Dim data As List
data.Initialize
Dim rs As ResultSet = B4XTable1.sql1.ExecQuery("SELECT * FROM data")
Do While rs.NextRow
  Dim row(6) As String
  row(0) = rs.GetString("c0")
  row(1) = rs.GetString("c1")
  row(2) = rs.GetString("c2")
  row(3) = rs.GetString("c3")
  row(4) = rs.GetString("c4")
  row(5) = rs.GetString("c5")
  data.Add(row)
Loop
rs.Close
Dim su As StringUtils
'rest of code
I will try it, thank you for your help.
 
Upvote 0
Top