Datasets

I am missing something fundamental about datasets. Here is what I’m trying to do:

I added a dataset as a dynamic property to the root container. The dataset has three columns (an int and two strings). I populate this dataset with a button event, and that part works fine. I can see the data in the dataset in the designer.

Now, I want to access the data dynamically. For instance, I would want the value in column 2 from the row where the int = 9 (there will never be duplicate values). I’ve tried a bunch of different things, but obviously I’m doing something wrong.

Also, just as an aside, is it possible to make the forum search a little less restrictive or at least make it an option to include any term I want? I tried to look up “dataset”, but can’t.

Ok, I think I’m getting this sorted out. A dataset is just a two-dimensional array, right? So, I can reference a specific item with “dataset[col][row]”, like this (substituting dynamic values for row and col in the real code):

	table = event.source.parent.Test.data
	col = 1
	row = 8
	print table[col][row]

Then, if I leave out [row], it returns a list of all values in the specified column:

	table = event.source.parent.Test.data
	col = 1
	row = 8
	print table[col]

So, are datasets just Python lists? Am I looking at them in the right way now?

Python doesn’t have datasets. But you can use the toPyDataSet function to convert a FPMI dataset to a ‘PyDataSet’. Then you can access the data like a dictionary or a tuple.

Check the manual for information on how to use the function fpmi.db.toPyDataSet and the fpmi.db.runQuery function shows how to access the data in the PyDataSet.

I saw that in the documentation, but this part made me think it would be the wrong approach: “Runs the SQL query against the project’s default datasource, or against the specified datasource if present. Returns the results as a “PyDataSet”, which is a Jython object that can be referenced like a tuple(sequence) of rows.” My data isn’t in a SQL database and isn’t run against the datasource. I just assumed that since the data is already an array of lists that I could use python to work with it without converting it.

A DataSet is the same thing whether it is manually entered (in your case, via a button) or pulled in from running a query. A DataSet isn't quite the same thing as a two dimensional array, althought they are similar. The difference is that a DataSet has the notion of named, typed columns, where a 2d-array doesn't. PyDataSets are much easier to use than DataSets in scripting, so you should convert it. The conversion is very lightweight, if you're worried about performance.

But, if you're really just looking to access a single piece of data in that dateset from column 2 where some other column has a value 9, you should look at the lookup expression.

Yeah, we'll have to look into that, that certainly wasn't intentional on our part.

Hope this helps,

I’ve got another question about datasets. Referring to this example from the help docs:

data = fpmi.db.runQuery(“SELECT Country, Population FROM MyTable”)
piechart = event.source.parent.getComponent(“PieChart”)
piechart.data = fpmi.db.toDataSet(data)

How would I so the same thing if the “piechart” was a dynamic property dataset instead of a component like a chart or listbox? Something like this:

data = fpmi.db.runQuery(“SELECT Country, Population FROM MyTable”)
piechart = event.source.parent.itemDataSet
piechart.data = fpmi.db.toDataSet(data)

I want the list to be populated by a button event, so I can’t bind it directly. I know I’m missing something very simple.

You were close, just over-thinking it a bit. The dynamic property would be the dataset, so you’d do something like this:

data = fpmi.db.runQuery("SELECT Country, Population FROM MyTable") event.source.parent.itemDataSet = fpmi.db.toDataSet(data)

Hope this helps,

Thanks, it works like a charm. I knew it would be simple, but I think I tried everything but that.

Maybe I am missing something here. I have tried to follow this post for some guidance on datasets. I have a dataset on a table. From this table I want to select the piece of data I want. Here is the code I have so far under the tables Mouse Clicked action:

[code]table = event.source.data
column = event.source.selectedColumn
row = event.source.selectedRow

if column != 3:
fpmi.gui.messageBox(“You are in the wrong column”)
else:
{this is where i am stuck}[/code]

I have tried using PRINT TABLE[1][1], I have even changed the data set over to a PYDATASET, but so far I not being able to pull the data from the selected row and column. I know it is something simple and have seen it here in the forums but for some reason the answer is eluding me. Thanks and have a great day.

To get the value out of that cell do the following:[code]data = event.source.data
column = event.source.selectedColumn
row = event.source.selectedRow

if column != 3:
fpmi.gui.messageBox(“You are in the wrong column”)
else:
value = data.getValueAt(row, column)
# do something with the value[/code]

This worked for me

[code]table = fpmi.db.toPyDataSet(event.source.data)
column = event.source.selectedColumn
row = event.source.selectedRow

if column != 3:
fpmi.gui.messageBox(“You are in the wrong column”)
else:
fpmi.gui.messageBox(str(table[row][column]))[/code]

Do you require that STR to be before the table? If so then that is what I was missing.

[quote=“Carl.Gould”]This worked for me

[code]table = fpmi.db.toPyDataSet(event.source.data)
column = event.source.selectedColumn
row = event.source.selectedRow

if column != 3:
fpmi.gui.messageBox(“You are in the wrong column”)
else:
fpmi.gui.messageBox(str(table[row][column]))[/code][/quote]

Well, if the datatype inside the dataset isn’t already a string, then yes.