Skip to content

How Impor a CSV file into table?

Forums Forums SIMPOL Programming How Impor a CSV file into table?

Tagged: 

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #3505
    jesusblues
    Participant

    Where can I find an example code to import a CSV file?

    #3506
    JD Kromkowski
    Participant

    Use “File>Import” in Personal

    Or is the actual code important? I don’t think that it is currently available to public but probably uses fsfileinputstream

    #3529
    jesusblues
    Participant

    I need create a routine for import a CSV file and integrate this code in my program.
    I nedd the code example.

    #3531
    JD Kromkowski
    Participant

    The code for dbconverter.sml doesn’t seem to be available. But if you add it to your project and take a look at dbCSVImport that might point you in right direction. I have no direct experience with this library.

    Without the dbconverter library, I’d think you’d start reinventing the wheel with something like

    fsfileinputstream f
    f =@ fsfileinputstream.new(“c:\mycvs.cvs”)

    Otherwise, you might ask Neil directly.

    #3534
    Michael
    Keymaster

    The following code demonstrates an import merge from CSV to SBME. It uses the dbconverter.sml, the address.sbm from the Address Book tutorial, and an address.csv containing 4 rows of data. Please note that there is no provision currently for assigning a value to a record ID type of field. Either the import data needs that information in the appropriate field, or some other mechanism would be required to assign the required information after the import is completed. Also, this example does not demonstrate how to use a field list to restrict or reorder the data to match the output/input. It assumes that every field is present and in the correct order in the import file.

    Ciao, Neil

    
    function main()
      sbme1 sbmfile
      sbme1table table
      integer e, rowsout
      string s
    
      e = 0
      sbmfile =@ sbme1.new("address.sbm", "O", error=e)
      if sbmfile =@= .nul
        s = "Error " + .tostr(e, 10) + " opening database file address.sbm{d}{a}"
      else
        table =@ sbmfile.opentable("Address", error=e)
        if table =@= .nul
          s = "Error " + .tostr(e, 10) + " opening table Address{d}{a}"
        else
          rowsout = csvimportmerge(table, e)
          if e != 0
            s = "Error " + .tostr(e, 10) + " importing data{d}{a}"
          else
            s = "Successfully imported " + .tostr(rowsout, 10) + " records{d}{a}"
          end if
        end if
      end if
    end function s
    
    function csvimportmerge(sbme1table table, integer error)
      dbCSVImport imp
      dbSBMEExport exp
      integer e, rowsout
    
      imp =@ dbCSVImport.new(",", "{d}{a}", 1, 2)
      imp.opensource("address.csv", "ISO-8859-1")
      exp =@ dbSBMEExport.new("O", .true, 1000)
      exp.sbme =@ table.sbme
      exp.sbmetable =@ table
      exp.createtable(table)
      exp.datasourcename = "Address@address.sbm"
      exp.merge = .true
      e = 0
      rowsout = exp.export(imp, error=e)
      if e != 0
        error=e
      end if
    end function rowsout
    
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.