I have a short script which saves a product name to a Azure SQL database. My question is how to test for and disallow duplicate names. I know I can put a constraint on the database which will throw an exception if a duplicate is entered. Also, I know I can use a “try … except” block to catch the exception. What I don’t know is how to identify that particular exception so it can be handled. Any ideas would be most appreciated! We are using Ignition Ver. 7.7.2, and here the script I’m using, which works fine except for the need to check for dups.
[code]varIng = event.source.parent.getComponent(‘Ingredient’).text
varIngID = event.source.parent.parent.SEL_ING
if (varIng == “”):
system.gui.messageBox(“Ingredient name cannot be blank”)
else:
if varIngID > 0:
#edit Ingredient
varIngID = system.db.runPrepUpdate(“UPDATE Ingredients SET INGREDIENT = ?
WHERE Ingrd_id = ?”, [varIng, varIngID])
else:
# Add Ingredient
varIngID = system.db.runPrepUpdate("INSERT INTO Ingredients (INGREDIENT) VALUES (?)",
[varIng], getKey=1)
system.db.refresh(event.source.parent.parent.getComponent(‘Ing_Tbl’),‘data’)
[/code]