# Problem with duplicates in MySql

**URL:** <https://forum.inductiveautomation.com/t/problem-with-duplicates-in-mysql/76086>\
**Category:** Ignition\
**Tags:** ignition81, vision, scripting\
**Created:** [July 7, 2023, 9:46pm UTC](https://forum.inductiveautomation.com/t/problem-with-duplicates-in-mysql/76086 "2023-07-07T21:46:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miguel\_Gomez](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Miguel\_Gomez](https://forum.inductiveautomation.com/u/Miguel_Gomez)\
**Post date:** [July 7, 2023, 9:46pm UTC](https://forum.inductiveautomation.com/t/problem-with-duplicates-in-mysql/76086/1 "2023-07-07T21:46:29Z")

</div>

Hello friends,

Im trying to send some data to a DataBase but the problem is that when the data is inserted it is savig 3 times the same information

Ok, this is what I have,

In a Vision Window I Have 2 calendar boxes, one with the current time and the other one fixed

The left box called time0 has this code in the Expression area: `now()`  
The box on the right, called time1 has this code: `dateArithmetic(dateFormat({Root Container.time0.date},"yyyy-MM-dd HH:mm:00"),1,'sec')`  
I did this because I need that each minute both date must be the same and when this happen some information must be inserted to the data base

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/4/94e6993d4b1f2ef0b66b2a85f2d32866de0c8edf.png)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/e/fe71989ef1fddab578cf56f62f34d8b7f0e7db09.png)

I have this code in the propertyChange of the time0 button (because it change each second so it can check each second if both dates are the same):

```auto
time0 = event.source.text
time1 = event.source.parent.getComponent('time1').text
value = event.source.parent.getComponent('LED Display').value
flowmeter = event.source.parent.getComponent('flowmeter').text
parameters = {"date":time1, "value":value,"flowmeter":flowmeter} 
if time0 == time1:
            system.db.runNamedQuery("Insert Data", parameters)

```

The Named Query is this

```auto
INSERT INTO _mydb
	( date_, meter_, volume_)
VALUES
	(:date , :flowmeter , :value )

```

But this is inserting in the database the same data 3 times

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/6/c65aaf9400273148e9d3d9b1daa70ddcfe306889.png)

So, I tried with a while statement trying to run just one time the query

```auto
if time0 == time1:
         i=0
         while i<1:
            system.db.runNamedQuery("Insert Data", parameters)
            i+=1

```

With the same results so that tell me that the script is running 3 times at the same time and no just one time.

So if you have some ideas I really appreciate that.

Thank you!

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [July 7, 2023, 9:54pm UTC](https://forum.inductiveautomation.com/t/problem-with-duplicates-in-mysql/76086/2 "2023-07-07T21:54:16Z")

</div>

Tip: edit your post (pencil icon), select each code block in turn and hit the \</\> button. This will preserve indents (essential for Python) and apply syntax highlighting and fixed font width. It will also be much easier to see where code starts and ends.

---

<div class="post-metadata">

**Author:** ![Miguel\_Gomez](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Miguel\_Gomez](https://forum.inductiveautomation.com/u/Miguel_Gomez)\
**Post date:** [July 7, 2023, 10:24pm UTC](https://forum.inductiveautomation.com/t/problem-with-duplicates-in-mysql/76086/3 "2023-07-07T22:24:47Z")

</div>

Thank you! 😃

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [July 7, 2023, 10:49pm UTC](https://forum.inductiveautomation.com/t/problem-with-duplicates-in-mysql/76086/4 "2023-07-07T22:49:13Z")

</div>

I can't see the problem exactly but I suspect that for the second that your `==` check is true that the `runNamedQuery` is repeatedly executing at a rate of three times per second. I would expect this to be due to the millisecond time chaning in the background and retriggering something but you can't see it because of your formatting.

You could try to sort this out with a "one-shot" tag something like this:

```auto
if time0 == time1:
    system.db.runNamedQuery("Insert Data", parameters)
    oneshot = True
else:
    oneshot = False

```

But I suspect that this is just masking poor logic design and there must be a much better way to achieve your goal.

* * *

Tip 2: For inline code enclose it in a pari of `` backticks. You'll then get `nice code formatting` in the middle of a sentence.

---

<div class="post-metadata">

**Author:** ![Miguel\_Gomez](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Miguel\_Gomez](https://forum.inductiveautomation.com/u/Miguel_Gomez)\
**Post date:** [July 7, 2023, 11:14pm UTC](https://forum.inductiveautomation.com/t/problem-with-duplicates-in-mysql/76086/5 "2023-07-07T23:14:37Z")

</div>

Thank you for your help! but the problem persist,

I'm trying right now another thing with the query and is checking first if exist a row with the same time and the same flowmeter so the first time the quiery will insert the data but not he other two times.  
I'm just searching how to write that

Thank you and if you have more ideas I really appreciate that!

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [July 8, 2023, 2:06am UTC](https://forum.inductiveautomation.com/t/problem-with-duplicates-in-mysql/76086/6 "2023-07-08T02:06:05Z")

</div>

> [@Miguel\_Gomez](#):
>
> But this is inserting in the database the same data 3 times

The propertyChange event handler runs once for _ **every** _ property change, and I don't see anything in your propertyChange script that limits which property change event fires your code. On a Vision popup calender, three properties change anytime the date changes: "date", "text", and "formattedDate," so pick one to qualify your script, and that will fix your issue.

**Example:**

```auto
if event.propertyName == 'date':
	#[nest your script here]

```

This way you will only get one database entry instead of three because your property change script will only run when the date property changes instead of also running when the text property changes and when the formattedText property changes.

---

<div class="post-metadata">

**Author:** ![Miguel\_Gomez](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Miguel\_Gomez](https://forum.inductiveautomation.com/u/Miguel_Gomez)\
**Post date:** [July 10, 2023, 4:09am UTC](https://forum.inductiveautomation.com/t/problem-with-duplicates-in-mysql/76086/7 "2023-07-10T04:09:42Z")

</div>

Thank you!,

It works! I changed my code to the propertyChange on the other calendar (time1) because it changes each minute and not each second

```auto
time1 = event.source.text
value = event.source.parent.getComponent('LED Display').value
flowmeter = event.source.parent.getComponent('flowmeter').text
parameters = {"date":time1, "value":value,"flowmeter":flowmeter} 

if event.propertyName == 'date': 
    system.db.runNamedQuery("Insert Data", parameters)

```

And now it is working

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/f/6f6c73cf2f307da1d9c7902f59cd3790c23f9394.png)

Thank you again!
