Article
· Jul 31 1m read

Import CSV into CACHÉ

Here's a practical example of how to import data from a CSV file into InterSystems CACHÉ using ObjectScript
Assuming your CSV file is simple (e.g., comma-separated, with headers), you can use %Stream.FileCharacter to read it line by line and parse the data.

 

ClassMethod ImportCSV(filePath As %String) As %Status {
    Set stream = ##class(%Stream.FileCharacter).%New()
    Set sc = stream.LinkToFile(filePath)
    If 'sc Quit sc

    While 'stream.AtEnd {
        Set line = stream.ReadLine()
        Set fields = $ListFromString(line, ",")
        // Example: Save to a persistent class
        Set obj = ##class(MyApp.Data).%New()
        Set obj.Name = $List(fields,1)
        Set obj.Age = $List(fields,2)
        Set obj.Email = $List(fields,3)
        Do obj.%Save()
    }
    Quit $$$OK
}

Discussion (0)1
Log in or sign up to continue