问题描述
我有一个表Table1
,其中包含Spotfire(dcube)中的一些上下文信息,其内容如下:
Name Food Seating
XYZ Seafood outdoors
我用两个按钮Yes
和No
创建了一个测试区域。
用户将在Table1
中选择一条记录,然后单击其中一个按钮,它应使用添加的列Table2
填充另一个表Preference
,其输出如下:
Name Food Seating Preference
XYZ Seafood outdoor Yes
根据用户是单击preference
还是Yes
来填充No
列的值。当用户为Table1
中的另一条记录设置首选项时,应在Table2
中将其追加为一行。
是否可以通过R或IronPython而不使用TERR?
解决方法
您可以使用python并写回数据库来实现此目的。但是我不确定在没有外部数据源的情况下执行此操作的良好/有效方法。
from Spotfire.Dxp.Data.Import import DatabaseDataSource
from Spotfire.Dxp.Data.Import import DatabaseDataSourceSettings
from Spotfire.Dxp.Application.Visuals import TablePlot
from Spotfire.Dxp.Application.Visuals import VisualTypeIdentifiers
from Spotfire.Dxp.Data import IndexSet
from Spotfire.Dxp.Data import RowSelection
from Spotfire.Dxp.Data import DataValueCursor
from Spotfire.Dxp.Data import DataSelection
from Spotfire.Dxp.Data import DataPropertyClass
rowCount = Document.ActiveDataTableReference.RowCount
rowsToInclude = IndexSet(rowCount,True)
#Get a cursor to the two columns we want to use. cursor1 is for the key column and cursor2 is for the column selected by the user input
cursor1 = DataValueCursor.Create[int](Document.ActiveDataTableReference.Columns["ProductID"])
cursor2 = DataValueCursor.CreateFormatted(Document.ActiveDataTableReference.Columns[whichCol])
#The following section will add a column to the database table using the name specified by the user. This assumes a column with this name does not already exist.
sqlCommand = "ALTER TABLE Products ADD " + colName + " varchar(50);"
dbsettings = DatabaseDataSourceSettings( "System.Data.SqlClient","Server=localhost;Database=myDB;UID=myuser;PWD=mypass",sqlCommand)
ds = DatabaseDataSource(dbsettings)
newDataTable = Document.Data.Tables.Add("temp",ds)
Document.Data.Tables.Remove(newDataTable)
#The following section will update the specified column in the database using the key column in the where clause
sqlStr=""
for row in Document.ActiveDataTableReference.GetRows(rowsToInclude,cursor1,cursor2):
value1 = cursor1.CurrentValue
value2 = cursor2.CurrentValue
sqlStr = sqlStr + "UPDATE Products SET " + colName + "='" + value2 + "' WHERE (ProductID=" + str(value1) + ");"
sqlCommand = "UPDATE Products " + sqlStr + ";"
dbsettings = DatabaseDataSourceSettings( "System.Data.SqlClient","Server=localhost;Database=Northwind;UID=myuser;PWD=mypass",sqlStr)
ds = DatabaseDataSource(dbsettings)
newDataTable = Document.Data.Tables.Add("temp",ds)
Document.Data.Tables.Remove(newDataTable)
以上内容来自Tibco Spotfire,我已将其用作模板,使用SQL Server中的表在Spotfire中创建票务系统。