Insert a row to a data table

from Spotfire.Dxp.Data import AddRowsSettings
import System
from System import DateTime
from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin
from Spotfire.Dxp.Data import DataType, DataTableSaveSettings
from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings

#First we need to create your data with some columns. 
#Here I have a comma separated miniature table built up with 
#a commented option for your variable itself. \r\n used for newline
data_for_new_row= "val1,val2,val3"
textData = "col1,col2,col3\r\n" + data_for_new_row + "\r\n"

#Memory Stream stuff. Simply just writing our variable 
#into a place we can access to make a data source
stream = MemoryStream()
writer = StreamWriter(stream)
writer.Write(textData)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)

#you need settings to tell the system what stuff you're importing.
#here we define it is comma separated and what data types our columns are.
readerSettings = TextDataReaderSettings()
readerSettings.Separator = ","
readerSettings.AddColumnNameRow(0)
readerSettings.SetDataType(0, DataType.String)
readerSettings.SetDataType(1, DataType.Integer)
readerSettings.SetDataType(2, DataType.String)
textDataSource = TextFileDataSource(stream,readerSettings)

#We create some settings here automatically having the system match
#column names for us between the data table and our data source.
settings = AddRowsSettings(dataTable,textDataSource)

#And finally we add the rows from our datasource with our settings.
dataTable.AddRows(textDataSource,settings)

Reference here

2 comments:

oneness said...

Its not working in webplayer, getting IO error. Please help me to update user comments into datatable.

Thanks,
Arun

Jose Leviaguirre said...

Hello oneness,

Try using an information link to insert by editing the query and adding parameters.