I am trying to use a gateway script triggered by the rising edge of a Boolean tag change to query a SQL database for a lot number. If a matching lot number is found within the past two years, I need to set another Boolean tag to “True”.
Below is my script:
from datetime import datetime, timedelta
from system.db import runQuery
if not initialChange and not currentValue.value == 0 and previousValue.value == 0:
# Get the value of the Scanned_Lot_Number tag
scannedLot = system.tag.readBlocking(["[default]PhibroTest/Scanned_Lot_Number"])[0].value
# Calculate the date 2 years ago
twoYearsAgo = datetime.now() - timedelta(days=730)
# Define the SQL Query (use a function to avoid variable issue)
def get_sql():
return "SELECT COUNT(*) FROM TestDB.dbo.TransactionReportHeader WHERE Lot_Number = ? AND t_stamp >= ?"
sql = get_sql()
# Execute the Query
try:
result = runQuery("TestDataBase", sql, [scannedLot, twoYearsAgo])
count = result[0][0]
isValid = count > 0
except Exception as e:
system.util.logError("Database Query Failed", e)
isValid = False
# Write the result to the Boolean Tag
system.tag.writeBlocking(["[default]PhibroTest/SCADA_Lot_Found"], [isValid])
Admittedly, SQL is not in my wheelhouse, so I am a little lost here. In the Gateway Scripts Logger, the script lists "Error" under status, gives the following error message, and then changes to "Success" but does not actually write "True" to the Boolean.
(This is abridged, but should be all the relevant information.)
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 19, in File "", line 19, in at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:392) at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runQuery(AbstractDBUtilities.java:359) at jdk.internal.reflect.GeneratedMethodAccessor117.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) java.lang.Exception: java.lang.Exception: Error executing system.db.runQuery(TestDataBase, Transaction datasource unknown, [u'0987654111', datetime.datetime(2024, 3, 11, 13, 13, 4, 64000)])
...
Caused by: org.python.core.PyException: java.lang.Exception: java.lang.Exception: Error executing system.db.runQuery(TestDataBase, Transaction datasource unknown, [u'0987654111', datetime.datetime(2024, 3, 11, 13, 13, 4, 64000)])
Caused by: java.lang.Exception: Error executing system.db.runQuery(TestDataBase, Transaction datasource unknown, [u'0987654111', datetime.datetime(2024, 3, 11, 13, 13, 4, 64000)])
Any help or guidance at all would be a huge help. Thanks in advance.