I am trying to update a power table data from a button in vision. So I have a table with 10 columns in my database now I want to select only 6 columns so I am doing this in a named query where I select 6 columns out of 10. Then I bind that data to a power table in vision. Now when the user selects a row in the table a specific cell of that table should be updated. Now for doing this I wrote my script on the action performed of a button:
Which updates the table but does not update the original SQL table. Not sure how to fix it. I have tried different things and updated query scripts as well but non of them worked.
No, youâre doing it well.
The problem is that you are not writing the actual UPDATE QUERY or at least this is what is seems itâs happening. Add the query on âonCellEditedâ Extension Function.
@jespinmartin1 So what I am doing exactly is I have a table with 10 columns in my database now I want to select only 6 columns so I am doing this in a named query where I select 6 columns out of 10. Then I bind that data to a power table in vision. Now when the user selects a row in the table a specific cell of that table should be updated. Now for doing this I wrote my script on a button:
dear @pkhoshroo ,
If that is your entire script, so I return to my 1st reply. You are not actually writing nothing to the database because you donât have any UPDATE QUERY.
The script you are showing just alters the data in you dataset. It doesnât mean it will be bidirectional to the table a the database. To actually change data at SQL table is by using an Update Query.
Yes, I know to use an Update query but I am not sure how to capture the selected row and column in the query and set that value and even if I should write that on the button or on the table. Since capturing the whole row is not possible in the query I need to write where col1=⌠and col2=⌠and ⌠col6=âŚwrite?
And for specifying column based on selected row I should write:
col1= event.source.parent.getComponent(âPower Tableâ).data.getValueAt(table.selectedRow, âcol1â)
This is SQL design. Your SQL table should have a Primary Key, this works like an unique id for each row. That is what you need to catch and use it in Update Query.
It doesnât matter if you use it in a button or in the Power Table itself
Note the id binding looks like this try({Root Container.Power Table.data}[{Root Container.Power Table.selectedRow},'id'],-1)
5.- Finally the script, in the button
updateQuery = \
"""
UPDATE table
SET %s = ?
WHERE id = ?
""" % event.source.selectedColumnName
newValue = event.source.newValue
id = event.source.id
if event.source.selectedColumnName not in ('id','') and id != -1:
system.db.runPrepUpdate(updateQuery,[newValue, id])
system.db.refresh(event.source.parent.getComponent('Power Table'),'data')
You can bind directly to the selectedColumn index, and from there it's trivial to convert the index into a column name. I suppose a custom property could be added that could be bound to. The custom property could then be automatically updated via a property change script.
Here are two ways that I know of to obtain the selected column name of a power table with scripting: