Database write in runscript

I have a shared script doing a few calculations based off some named query calls (database reads).
I’m accessing the script via expression tag -> runscript. In this script, I’m also gathering some other information that I want to store to a database table every 15 minutes of the hour. This section of code will iterate and write to the database but it seems to only do it whenever it wants and not on 15 minute intervals as coded. The database write will work in the scripting console just as I want it to but not using the runscript. I’m wondering if this has something to do with scan classes? or perhaps the difference in time between the database/client/gateway? I’ve attached the write code.

**#determine if in 15 minute interval**

** if currentMinute % 15 == 0 : **
** try:**
** param4 = {“date”:spillLogTime,“sluice”:sluice,“headWater”:headWater,“gate1pos”:gate1pos,“gate2pos”:gate2pos,“gate3pos”:gate3pos,**
** “gate1boards”:gate1boards,“gate2boards”:gate2boards,“gate3boards”:gate3boards,“gen1setting”:gen1setting,“gen2setting”:gen2setting,**
** “gate1flow”:gateFlow[0],“gate2flow”:gateFlow[1],“gate3flow”:gateFlow[2],“gatesTotalFlow”:gateFlowTotal,“genTotalFlow”:combinedGenFlow,“totalFlow”:totalFlow,“site”:site} **
** q=system.db.runNamedQuery(“GBRA_Hydro_Data_Collection”,“Insert Spillway Flow”,param4)**
** except:**
** print “The Record Already exists”**

version is 7.9.11

using MySQL on the local develop machine
using MS SQL on the deployed machine

both have same issue

When your script is called from the runScript expression there’s no project and no default datasource defined because it’s running from the context of a tag.

That said, this is an abuse of runScript and you should really just be doing this in a gateway timer script.

That has been addressed when adding the project name and default datasource to the named query call and tag read respectively. I had a conversation with tech support about that last week. The reason for doing the database write in this particular spot is to grab all the other calculations being generated in the rest of this script. I do agree that it is an abuse of the runscript, however it made the most logical sense in order to not have the same pieces of code in more the one place.

I actually got it working sort of now.

Now the issue im having is only writing to the database once in that minute instead of every time its evaluated

I would move everything to gateway timer scripts. Do your calculations at whatever frequency you want and then write to some memory tags holding the values calculated in the script. Every 15 minutes you can write to the database.

I find it difficult to believe that tech support would opt to do this in an expression rather than in a project or global script. One of the main purposes of shared scripts is to avoid code duplication.

I would recommend the use of a trigger to indicate to the database write that the calculations have been completed and it is okay to go forward with the write. Basically handshaking between the two scripts, that will allow the calculation and write scripts to be on different intervals.