# Save information automatically at the end of the count

**URL:** <https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400>\
**Category:** Ignition\
**Tags:** ignition78, ignition79, ignition80, ignition81, database\
**Created:** [April 8, 2021, 4:53pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400 "2021-04-08T16:53:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 8, 2021, 4:53pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/1 "2021-04-08T16:53:24Z")

</div>

Hello everyone

I have an Expression TAG that keeps me counting how much a Boolean Opc tag is activated and for when it is deactivated.

and I require that at the time of deactivation I save the information in a SQL database

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/b/2b89f79e24f29129cce851fa6d9f1349b7e354d3.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/1/31c75794ceac86e4c23c4a5812416485107b65e3.png)

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [April 8, 2021, 5:30pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/2 "2021-04-08T17:30:53Z")

</div>

This seems like a task for a transaction group. You can set them to trigger on an event (tag deactivation in your case).

User manual pages for transaction groups: [SQL Bridge (Transaction Groups) - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/pages/viewpage.action?pageId=58597803)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 9, 2021, 11:17am UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/3 "2021-04-09T11:17:58Z")

</div>

I can see 3 issues with your code, but what was your question?

The issues I see are:

1. you need to exclude `initialChange` events in your first if conditions
2. you’re supplying the query with a QualifiedValue (Contador). You need to add `.value` to the end to read the value of the tag
3. you’re executing the insert statement whenever the tag changes value, not only when it is False

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 9, 2021, 5:28pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/4 "2021-04-09T17:28:20Z")

</div>

> [@nminchin](#):
>
> you’re executing the insert statement whenever the tag changes value, not only when it is False

ok, I understand and how do you go to make the change on and off that the label does

do you have any example?

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 9, 2021, 8:51pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/5 "2021-04-09T20:51:18Z")

</div>

If I understand your question correctly,

```auto
if currentValue.value == False and currentValue.value != previousValue.value and not initialChange:

```

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 9, 2021, 9:58pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/6 "2021-04-09T21:58:59Z")

</div>

send me the following error “mismatched input " expecting DEDENT”

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/0/90e9bd582fb3dff62b6c52e620c7e9532cf25474.png)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 9, 2021, 10:06pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/7 "2021-04-09T22:06:46Z")

</div>

Python syntax requires all your line indents to be right. Your if statement is on a different indent to the function’s description string above. You need to push the if statement back to the same column as the line above

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 9, 2021, 10:29pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/8 "2021-04-09T22:29:25Z")

</div>

I have a new error

([default]CONTEO DE FALLA/Falla safety fance open 1, valueChanged) Error executing tag event script: Traceback (most recent call last): File “tagevent:valueChanged”, line 8, in valueChanged at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362) at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:258) at sun.reflect.GeneratedMethodAccessor52.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?,?), [Item, Prueba, Prueba, 0.0, Fri Apr 09 17:25:51 CDT 2021], TOPY\_MW, , false, false)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 10, 2021, 12:02am UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/9 "2021-04-10T00:02:46Z")

</div>

You have an extra question mark in your query.

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 12, 2021, 5:49pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/10 "2021-04-12T17:49:53Z")

</div>

I have a new error

```auto
if currentValue.value == False and currentValue.value != previousValue.value and not initialChange:
					INS = "INSERT INTO PAROS ( Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES ( ?, ?, ?, ?, ?)"
								
					Contador = system.tag.read("[.]MemTimeDiff")
					Fecha = system.date.now()
								
					system.db.runPrepUpdate(INS,["Item", "Prueba", "Prueba", Contador.value, Fecha],"TOY");

```

> ([default] FAIL COUNT / Fail safety fance open 1, valueChanged) Error executing tag event script: Traceback (last most recent call): File "\<tagevent: valueChanged\>", line 8, in valueChanged in com .inductiveautomation.ignition .common.script.builtin.AbstractDBUtilities.error (AbstractDBUtilities.java:362) at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate (AbstractDBUtilities.java:258) at sun.java:258 : 258) Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown source) at java.lang.reflect.Method.invoke (Unknown source) java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO PAROS ( Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES ( ?, ?, ?, ?, ?), [Item, Prueba, Prueba, 0.21695000330607098, Mon Apr 12 13:45:24 CDT 2021], TOY, , false, false)

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [April 12, 2021, 6:58pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/11 "2021-04-12T18:58:47Z")

</div>

Post your code and the full error (little blue “full” link in the error popup window). Please use the “Preformatted Text” button (or ``` before and after your code/error snippets) to format your code/errors so that they are easier to read/decipher.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 12, 2021, 7:48pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/12 "2021-04-12T19:48:55Z")

</div>

The error to the PrepUpdate you’re posting doesn’t match your code.

Your code:

```auto
INSERT INTO PAROS ( Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES ( ?, ?, ?, ?, ?)

```

The Error:

```auto
INSERT INTO STOP (Line, Machine, StopType, StopTime, Date) VALUES (?,?,?,?,?)

```

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [April 12, 2021, 9:33pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/13 "2021-04-12T21:33:36Z")

</div>

Just a heads up that we do not get notifications when you edit your replies so you’re a lot less likely to get responses since we have to click on the topic to see if you’ve made any edits. You should really put your responses in a new reply. This will move your topic to the top of the list for us to see and will make it much easier for anyone else who reads this topic in the future to follow the history of replies.

It looks like your error still doesn’t match your script. Your script has “TOY” for the db connection in the `runPrepUpdate()` function while your error has “TOPY\_MW”. Also, that still doesn’t look like the full error trace…

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 12, 2021, 9:37pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/14 "2021-04-12T21:37:18Z")

</div>

I am creating an event where for a timer, where when stopping the timer it saves the data in my database.

but at the moment of calling the insertion of the data it marks the following error.

> Caused by: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO PAROS ( Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES ( ?, ?, ?, ?, ?), [Item, Prueba, Prueba, 0.21695000330607098, Mon Apr 12 13:45:24 CDT 2021], TOY, , false, false)

This is my code:

```auto
if currentValue.value == False and currentValue.value != previousValue.value and not initialChange:
                    INS = "INSERT INTO PAROS ( Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES ( ?, ?, ?, ?, ?)"
                                
                    Contador = system.tag.read("[.]MemTimeDiff")
                    Fecha = system.date.now()
                                
                    system.db.runPrepUpdate(INS,["Item", "Prueba", "Prueba", Contador.value, Fecha],"TOY")

```

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [April 12, 2021, 9:41pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/15 "2021-04-12T21:41:38Z")

</div>

Delete the semi-colon at the very end of your script.

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 12, 2021, 9:54pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/16 "2021-04-12T21:54:03Z")

</div>

I think I already know that my “counter” variable captures a string and wants to save it as time in the database

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [April 12, 2021, 9:59pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/17 "2021-04-12T21:59:40Z")

</div>

If you send the full error message, we can offer more help. Earlier, I forgot you are running this script from a tag event (earlier instructions were for error popups when running the script in the designer/client). To get the full error message from the gateway log you need to go to your Gateway Webpage \>\> Status \>\> Logs, find your error, and click the “+” sign to the right of the error message and copy/paste that full error message here.

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 12, 2021, 10:03pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/18 "2021-04-12T22:03:44Z")

</div>

> com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 8, in valueChanged at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362) at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:258) at sun.reflect.GeneratedMethodAccessor52.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?), [Item, Prueba, Prueba, 0.03338332970937093, Mon Apr 12 16:58:51 CDT 2021], TOY, , false, false)

at org.python.core.Py.JavaError(Py.java:495)

at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362)

at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:258)

at sun.reflect.GeneratedMethodAccessor52.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at org.python.core.PyReflectedFunction. **call** (PyReflectedFunction.java:186)

at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction. **call** (ScriptManager.java:431)

at org.python.core.PyObject. **call** (PyObject.java:422)

at org.python.core.PyObject. **call** (PyObject.java:426)

at org.python.pycode.\_pyx96381138.valueChanged$1(:8)

at org.python.pycode.\_pyx96381138.call\_function()

at org.python.core.PyTableCode.call(PyTableCode.java:165)

at org.python.core.PyBaseCode.call(PyBaseCode.java:301)

at org.python.core.PyFunction.function\_\_\_call\_\_(PyFunction.java:376)

at org.python.core.PyFunction. **call** (PyFunction.java:371)

at org.python.core.PyFunction. **call** (PyFunction.java:361)

at org.python.core.PyFunction. **call** (PyFunction.java:356)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:649)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$FunctionInvokerImpl.run(TagScriptManager.java:493)

at com.inductiveautomation.ignition.common.sqltags.scripts.AbstractTagScript.invoke(AbstractTagScript.java:33)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$Task.invoke(TagScriptManager.java:442)

at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$TagScriptDispatcher.run(TagScriptManager.java:405)

at com.inductiveautomation.ignition.common.execution.impl.BasicExecutionEngine$ThrowableCatchingRunnable.run(BasicExecutionEngine.java:518)

at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

at java.util.concurrent.FutureTask.run(Unknown Source)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)

at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 8, in valueChanged at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362) at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:258) at sun.reflect.GeneratedMethodAccessor52.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?), [Item, Prueba, Prueba, 0.03338332970937093, Mon Apr 12 16:58:51 CDT 2021], TOY, , false, false)

... 31 common frames omitted

Caused by: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?), [Item, Prueba, Prueba, 0.03338332970937093, Mon Apr 12 16:58:51 CDT 2021], TOY, , false, false)

... 30 common frames omitted

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Operand type clash: float is incompatible with time

at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)

at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1454)

at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:388)

at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:338)

at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)

at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)

at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:185)

at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:160)

at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:306)

at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)

at com.inductiveautomation.ignition.gateway.datasource.SRConnectionWrapper$SRPreparedStatement.executeUpdate(SRConnectionWrapper.java:955)

at com.inductiveautomation.ignition.gateway.datasource.SRConnectionWrapper.runPrepUpdate(SRConnectionWrapper.java:180)

at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities.\_runPrepStmt(GatewayDBUtilities.java:132)

at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:256)

... 28 common frames omitted

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [April 12, 2021, 10:56pm UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/19 "2021-04-12T22:56:41Z")

</div>

`Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Operand type clash: float is incompatible with time`

You do have a type compatibility issue but it’s not due to a string, but rather a float trying to write to a Time column. I’m assuming your `TiempoParo` column is a Time datatype and you are trying to write a float value (0.03338…) to it.

Edit: Looks like you already figured this out based on your [new topic](http://forum.inductiveautomation.com/t/change-string-data-to-time/45510). You really should keep these together so that people have context, and those trying to help you (like me) don’t spend time replying when you clearly already know the problem…

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 13, 2021, 4:23am UTC](https://forum.inductiveautomation.com/t/save-information-automatically-at-the-end-of-the-count/45400/20 "2021-04-13T04:23:52Z")

</div>

FYI You really need to put anything that requires formatting to read (eg python or errors) into preformatted blocks, otherwise it’s incredibly difficult to read. Kudos to @WillMT10 for finding what he did!
