Importar CSV en CACHÉ
Aquí tenéis un ejemplo práctico de cómo importar datos desde un archivo CSV a InterSystems CACHÉ utilizando ObjectScript.
Suponiendo que vuestro archivo CSV sea simple (por ejemplo, separado por comas y con encabezados), podéis usar `%Stream.FileCharacter` para leerlo línea por línea y analizar los datos.
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
}