I’m trying to update the row using the button in perspective. Please can you suggest to me what is worng from my side?
date = self.getSibling(“TextField”).props.text
if date == None:
id = self.view.params.ID
system.db.runPrepUpdate(‘UPDATE sample SET date = now() where id = ?’, [id])
@victordcq Thank you for giving me a quick response. I’m trying to update the row using the button. when I have run the script then not found any error still not updated the row. I’m trying to update the current date when the date IS NULL using the button. I’m using MySQL database. and textfield used for display date column value in column container perspective.
To add to @victordcq answer, you can get away with:
if not date:
(do something)
None, 0, empty strings, etc. will return false when checked in an if statement. Also, be aware that if x == None can be fooled. The preferred way is to use if x is None
I think my issue is the DateTime format. MySQL database DateTime format some think like this →
2021-08-18 13:00:35. textfield DateTime format → " 2021-07-20T10:37:43.000Z" so not update row when run the script.
Formatting dates before inserting them is always the wrong move.
You’re already using runPrepUpdate, so the correct move would be to use parameterized substitution to insert a date value.
However, as written in your first post, you’re not inserting a date from Ignition at all; you’re using the function now()on the database to generate a timestamp. Ignition’s date formatting is not relevant at all in that case.
You just kicked the problem down the road a bit. As soon as you start working with multiple time zones, this will likely break. Learn to pass dates and strings and numbers through JDBC as parameters. If string formatting is needed to make your queries work, you are doing it wrong.
Good point. On that - and sorry to hijack somewhat… - how do you handle timestamps in the PLC? should PLC RTCs all be set to UTC as well as all timestamps written to SQL? (e.g. in unix epoch format)