Problem with duplicates in MySql

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

image

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):

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

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

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

image

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

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!

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.

2 Likes

Thank you! :smiley:

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:

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.

1 Like

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!

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:

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.

2 Likes

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

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

Thank you again!

1 Like