iOS Question B4i - SQL Tables to CSV

asmondien

Member
Licensed User
Longtime User
Hi -

I'm re-posting the post as it was deleted by Erel for thinking I posted it in wrong forum.

I have small application that I wrote in b4i and I have 4 tables that I would like to export to CSV. I have found couple posts :


I have tried to use stringutils to save a list to CSV as suggested, but that I got errors - after bit of searching, it's recommend to the use CSV Parser.

While I can save bit of couple columns of the table to CSV, it won't save all rows :

B4X:
    Dim myParser As CSVParser
    myParser.Initialize
    Dim myData As List
    myData.Initialize
    
    Dim rs As ResultSet = mySQL.ExecQuery("SELECT * FROM Retailer")

    
    Do While rs.NextRow
        Dim t As String = rs.GetString("retID") & "," & rs.GetString("retName") & ","  & rs.GetString("retLocation")

        Dim myTable As List = myParser.Parse(t, ",",False)
        For Each row() As String In myTable
            'work with row
        Next
        Dim s As String = myParser.GenerateString(myTable,",")
        
    Loop
    rs.Close
    File.WriteString(File.DirDocuments, "table.csv", s)

I understand that I'm overwriting S with another line, which is my problem - but I can't seem to save the list of rows to the CSV + I wasn't able to save all my columns - do I need to worry about special characters with this parses?

Hope you can help.

Thanks

Jiri
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I have tried to use stringutils to save a list to CSV as suggested, but that I got errors - after bit of searching, it's recommend to the use CSV Parser.
Both options will work fine.

I don't understand your code. You need to collect all rows into a list and only then call GenerateString. After the Do While rs.NextRow loop.
 
Upvote 0

asmondien

Member
Licensed User
Longtime User
I think I miss understood the example - I thought I need to parse the string to the table(list) and then run the GenerateString to stuff if in.

So I made the change and I have the following code:

B4X:
Dim myParser As CSVParser
    myParser.Initialize
    Dim myData As List
    myData.Initialize
    
    Dim rs As ResultSet = mySQL.ExecQuery("SELECT * FROM Retailer")

    Do While rs.NextRow
        myData.Add(rs.GetString("retID"))
        myData.Add(rs.GetString("retName"))
        myData.Add(rs.GetString("retLocation"))
    Loop
    rs.Close
    Dim s As String = myParser.GenerateString(myData,",")
    File.WriteString(File.DirDocuments, "table.csv", s)
    Log("done")

But then I get error on line 18 in CSVParser:

B4X:
Application_Start
Should have DB
Application_Active
Error occurred on line: 18 (CSVParser)
-[NSTaggedPointerString getObjectFast:]: unrecognized selector sent to instance 0xa180807bf4b5ffb9
Stack Trace: (
  CoreFoundation       50CF3336-313F-3A7D-9048-CB1ED8EC3368 + 1222568
  libobjc.A.dylib      objc_exception_throw + 56
  CoreFoundation       50CF3336-313F-3A7D-9048-CB1ED8EC3368 + 192280
  CoreFoundation       50CF3336-313F-3A7D-9048-CB1ED8EC3368 + 1239196
  CoreFoundation       _CF_forwarding_prep_0 + 92
  myRetailer           -[b4i_csvparser _generatestring:::] + 1400
  CoreFoundation       50CF3336-313F-3A7D-9048-CB1ED8EC3368 + 1247600
  CoreFoundation       50CF3336-313F-3A7D-9048-CB1ED8EC3368 + 7088
  myRetailer           +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1608
  myRetailer           -[B4IShell runMethod:] + 448
 myRetailer           -[B4IShell raiseEventImpl:method:args::] + 1648
 myRetailer           -[B4IShellBI raiseEvent:event:params:] + 1580
 myRetailer           __33-[B4I raiseUIEvent:event:params:]_block_invoke + 60
 libdispatch.dylib    652A10DA-A0E7-3B47-9C95-AF3DFE45A66C + 371112
 libdispatch.dylib    652A10DA-A0E7-3B47-9C95-AF3DFE45A66C + 374052
 libdispatch.dylib    652A10DA-A0E7-3B47-9C95-AF3DFE45A66C + 54708
 CoreFoundation       50CF3336-313F-3A7D-9048-CB1ED8EC3368 + 690172
 CoreFoundation       50CF3336-313F-3A7D-9048-CB1ED8EC3368 + 669392
 CoreFoundation       CFRunLoopRunSpecific + 424
 GraphicsServices     GSEventRunModal + 160
 UIKitCore            UIApplicationMain + 1932
 myRetailer           main + 124
 libdyld.dylib        3D6D64B4-CB2B-3CC4-A7E9-02774DF7AE74 + 6384
)

I must be doing something wrong?

Thanks
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…