Writting database table using power table

Hi All
I created power table in vision to show table in database.
I want to modify database table by editing cells in power table
I can view table but not able to modify it on vision.
I m using below script associated on power table " onCellEdited" function

Get the id of the row we edited.

id = self.data.getValueAt(rowIndex, 'id')
 
# Create our query and arguments. The extension function gives us a colName variable,
# which we can use in our query. The query will then take two arguments.
# The value that we are updating and the id of the row we edited.
query = "UPDATE test SET %s = ? WHERE id = ?" % (colName)
args = [newValue, id]
 
# Run the query with the specified arguments.
system.db.runPrepUpdate(query, args)
 
# Requery the database, so we can ensure it properly updated the table.
system.db.refresh(self, "data")

I don't know how to help, but I thought I should point out id is a keyword in python, and you shouldn't use it as a variable name:

https://docs.python.org/3/library/functions.html#id

1 Like

When you construct SQL with string interpolation for identifiers (table names, column names, etc), you must pay extra attention to identifier quoting in case the supplied column names have spaces or their case is not consistent with the DB's defaults.

Also note that those database operations are not permitted in a Vision Client unless you have Legacy Database permission turned on in the project properties.

2 Likes

Thanks,
I have modified the code as below but no success. I changed id to ids and enable permission. I used to read rowIndex and colName by writting data to some other variable, it working fine. Only thing is query system.db.runPrepUpdate not working
ids = self.data.getValueAt(rowIndex, 0)
query = "UPDATE test SET %s = ? WHERE ids = ?" % (colName)
args = [newValue, ids]
system.db.runPrepUpdate(query,args)
system.db.refresh(self,"data")

What is you're indication that it isn't working? Is the data not updated, or do you get an error? If so what is the error?

2 Likes

Hi..
I am not getting any error. Data is not updated.

Is the data not updated in the DB or in Ignition?

Data is not updated in Ignition as well as in DB.
i can update dataset by using below script but goal is to update table in DB
self.data = system.dataset.setValue(self.data, rowIndex, colIndex, newValue)

But you're not seeing any errors at all pop up? Normally they would. I think sometimes in these table extensions functions the errors go straight to the console and not as a pop-up - do you see any errors there regarding SQL/java.lang.Exception? If it's really not updating in the db for some reason and the db is complaining, that should bubble up to Ignition.

If you are in fact getting no errors then this suggests that ids as provided to the query is not in the table.

If you run a select query in the DBMS

SELECT * from test WHERE ids = 'the ID you're looking for'

does it return rows?

Also, looking back at your code I believe you've messed up your query in trying to follow @pascal.fragnoud's advice.

id is a keyword in python if that is the name of the column in the sql db then you should still be using id in the where clause.

ids = self.data.getValueAt(rowIndex, 0)
query = "UPDATE test SET %s = ? WHERE id = ?" % (colName)
args = [newValue, ids]
system.db.runPrepUpdate(query,args)
system.db.refresh(self,"data")
2 Likes

Here is a working example from one of my tables.

	ValueToSet = event.value
	TableIndex = self.custom.SelectedID
	TableRow = self.custom.SelectedColumn
	query = "UPDATE equipment SET %s = ? WHERE id = ?" % (TableRow)
	args = [ValueToSet, TableIndex]
	 
	# Run the query with the specified arguments.
	system.db.runPrepUpdate(query, args)
1 Like

ids is returning row no. I tested and works fine

i revert back to code as below and column with id name not in table.
no success

id = self.data.getValueAt(rowIndex, 'id')
query = UPDATE test SET %s = ? WHERE id = ? % (colName)
args = [newValue, id]
system.db.runPrepUpdate(query, args)
system.db.refresh(self, data)

You should not use id in python script. Using it as a column header in the SQL table is not going to hurt anything. The SQL must match the Table Schema. Is the column in the table id or ids?

When you pull data from the data set, the column name you're pulling must match the name of the column in the dataset. Is the column name in the dataset id or ids

Please use the pre-formatted text option to post code. It's the button that looks like </>

Assuming that the Column is actually id in both the SQL table and the vision table then your script should look like:

index = self.data.getValueAt(rowIndex, 'id')
query = 'UPDATE test SET %s = ? WHERE id = ?' % (colName)
args = [newValue, index]
system.db.runPrepUpdate(query,args)
system.db.refresh(self,data)
1 Like

I have no vision experience but how does the query know what 'colName' and 'newValue' is ?

1 Like

This script is in the onCellEdited extension function, so those values are provided.

3 Likes

Thanks...Its done... :grinning: :grinning:

id is column name. newValue is coming with extension function, u do not need to write script.