Can a sql update be done on a table in perspective

It should be self.data.getValueAt. I think you didn’t save it. There is no way it’s telling you event.source is still throwing an attribute error if you replaced event.source.data with self.data

Actually, the answer has been both pointed out and given. Twice.

Perhaps you should read all of the replies.

You can not use event.source.* in perspective. You must do something similar to what @bkarabinchak.psi already showed.

Try using the property explorer in the script console to select the property that you want. It will give you the correct path.

Also, I would be careful with the snarky comments to those who are legitimately trying to help, their help may disappear altogether.

2 Likes

Didn't think I needed to since @code_skin provided that a little while ago.

And I find myself less inclined to help those who sneer at those who were trying to help.

Bye!

1 Like

This is how the line reads now.

id = self.data.getValueAt(event.row, 'wonum')

is this not correct? this is what I am seeing that I am being told to have.

Also yes, I have saved the project several times just to make extra sure it had been saved.
Still receiving the line 3 error.

Error running action ‘component.onEditCellCommit’ on Pages/Downtime POD/Downtime COS POD@C/root/Table: Traceback (most recent call last): File “function:runAction”, line 3, in runAction AttributeError: ‘com.inductiveautomation.perspective.gateway.script’ object has no attribute ‘data’

AttributeError: ‘com.inductiveautomation.perspective.gateway.script’ object has no attribute ‘data’

See you said it was the same error but this is actually different. Prior, it was an AttributeError that there was no source, but now it at least recognizes self, but there is not .data. And that's my fault, I think it should be self.props.data but take @lrose suggestion and use the property explorer to select the data property - you should always use that thing to grab what property you can use as python will not tell you until you run it that you have a typo. So you're much better off letting Ignition do the typing of component paths.

2 Likes

Thanks for catching that the error was different, I was in a bit of a hurry. Also thanks for the clear instruction on how to resolve the script. That has resolved the issue on line 3 and I was able to use the instruction to resolve the next issue I had on line 6.

My next error is coming on line 10 where I am getting a type mismatch. the column I am using for my ‘id’ is a string and the script is looking for an integer.

Is there a work around for this. here is line 10
query = “UPDATE MaintDTReport SET %s = ? WHERE ‘wonum’ = ?” % (headers[event.column])

and here is the error.
com.inductiveautomation.ignition.common.script.JythonExecException
Traceback (most recent call last):
File “function:runAction”, line 10, in runAction
TypeError: list indices must be integers

caused by org.python.core.PyException

Traceback (most recent call last):
File “function:runAction”, line 10, in runAction
TypeError: list indices must be integers

event.column is returning the name of the column header so you probably just need

query = "UPDATE MaintDTReport SET %s = ? WHERE 'wonum' = ?" % (event.column)

The next error you will run into is line 11 because event.newValue is another Vision artifact.

Thanks, that got line 10. you are also correct about 11 as well as I expect on error on all the remaining lines. it looks like I have gotten 11 working so now on to line 14.

Thanks everyone for the help! This is the final script that was successful.

1 Like

It worked a couple times then stopped. not sure what happened. I am getting an error for line 14. it is saying the column ‘owner’ is invalid. kinda perplexed as owner is the column name.

caused by org.python.core.PyException
Traceback (most recent call last):
File “function:runAction”, line 14, in runAction
java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(UPDATE MaintDTReport SET [‘owner’] = ? WHERE wonum = ?, [7, WO18240914], Indusoft_SQL_DB, , false, false)

caused by Exception: Error executing system.db.runPrepUpdate(UPDATE MaintDTReport SET ['owner'] = ? WHERE wonum = ?, [7, WO18240914], Indusoft_SQL_DB, , false, false)
caused by SQLServerException: Invalid column name ''owner''.

Looks like the problem is that somehow the column name is being passed with quotes around it. In SQL the column name [‘owner’] is not the same as [owner].

I’m not sure how that is occurring.

Yes I agree. also not sure how it is occurring. is it possible in the headers = lookup? idk. here is the script if there are any suggestions that can be made.

id = self.props.data.getValueAt(event.row, 'wonum')
	 
	# Get the header names of the dataset.
headers = self.props.data.getColumnName(event.row)
	 
	# Build our Update query. The column name is substituted in from the column that was edited.
	# The query will take two arguments. The value we are updating and the id of the row we are editing.
query = "UPDATE MaintDTReport SET %s = ? WHERE wonum = ?" % [event.column]
args = [event.row, 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.
self.refreshBinding("self.props.data")

In the script before you call the query, what is output if you print the event.column to the console?

system.perspective.print(event.column)
system.perspective.print(query)

you will have to excuse my lack of skills here, but is this what you are looking for?

22:08:48.902 [Browser Thread: 59201] INFO Perspective.Designer.Workspace - owner
22:08:48.902 [Browser Thread: 59201] INFO Perspective.Designer.Workspace - UPDATE MaintDTReport SET [‘owner’] = ? WHERE wonum = ?

Hmm, that seems odd.

Just for the hey of it, what happens if you do this:

query = "UPDATE MaintDTReport SET ” + event.column + “= ? WHERE wonum = ?"

Do you still get the error? It shouldn’t matter, but perhaps it does?

actually I removed the from around event.column and the error has moved to line 21.

aused by org.python.core.PyException
Traceback (most recent call last):
File “function:runAction”, line 21, in runAction
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Invalid property key - no scope defined: self.props.data

caused by IllegalArgumentException: Invalid property key - no scope defined: self.props.data

this is line 21

self.refreshBinding(“self.props.data”)

Yep, that should be.

self.refreshBinding(“props.data”)

The scope to this component is already established with the first self

does this error mean anything to you?

org.python.core.PySyntaxError
SyntaxError: (u"no viable alternative at character ‘\u201c’", (‘function:runAction’, 21, 21, u’\tself.refreshBinding(\u201cprops.data\u201d)\n’))

Ignition v8.1.3 (b2021030309)
Java: Azul Systems, Inc. 11.0.9

Yeah, you need to change the quotes around the string. Since I’m posting from my phone you’re not getting the correct quotes.

I get no error in designer and I don’t see any errors in the server log. cell value isn’t changing after committed.