Updating a query from a table

I have table that is bound to a database but i want to be able to edit any cell and have it update the database. Im using the onEditCellCommit action of the table and have this coded so far.
Def runAction(self, event):

valueToSet = event.value

self.props.data = system.dataset.setValue(self.props.data, event.row, event.column, valueToSet)

query= "EditData"

system.db.runNamedQuery(query)

And my query looks like this
UPDATE EvisData1

Set
Trailer = :Trailer,
Batch = :Batch

Right now it just deletes the data. I was trying to copy the data entry example from the example quick start. Im very new to scripting so i couldnt really follow how they were doing it. Any suggestions would be appriciated!

You're not passing any parameters to your query, so it just sends nulls instead.
You need to pass a dict containing keys that match your query parameters:

params = {
  'Trailer': value_for_trailer,
  'Batch': value_for_batch
}
system.db.runNamedQuery(query, params)

Also, take a look at this:

What do i have to pass to the query to allow the values to stay. I need to define the value_for_trailer but how do i define it for whatever value is entered in the cell?

Get the data from the dataset, using the selected row and and column like you did to update the dataset.

Your original query should be returning the id column (or whatever the unique key is for each row) and pass that back to the UPDATE query so that it knows which row to update. It would then look something like this:

UPDATE EvisData1
SET
    Trailer = :Trailer,
    Batch = :Batch
WHERE
    id = :id

The id column can be displayed in the table (handy for debug) or not.

# Get the table component
	table = self.parent.parent.getChild("Table")

# Get the selected row index
	row = table.props.selection.selectedRow

# Check if a row is selected
	if row is not None:
    # Get the data from the selected row
    		rowData = table.props.data[row]

    # Populate input fields with selected row data
    	self.parent.parent.getChild("ColumnContainer").getChild("BatchInput").props.value = rowData

I Couldnt get it with the editcellcommand so im trying with an edit button but its saying rowdata = table.props.data[row] is unscriptable. What am i doing wrong?

Once you've updated the database, you need to update the binding on the table to get it to pull new data containing the just edited values.

self.refreshBinding('props.data')

Shouldn't be necessary, since it seems like he's updating the database from the data in the table.

table.props.data is a dataset. You can't use the [] operator on those.
You need to use table.props.data.getValueAt(row, col)

1 Like