Error when trying to insert data in db mysql

Hello, I’m having a issue when I’m trying to insert some data into a DB made in mysql,… I’m using the system.db.runPrepUpdate… well the scrip I’m trying to use is this…

system.db.runPrepUpdate("INSERT INTO visiontable (Modelo) VALUES (?)",['5']);

I’m using this script in a momentary button just to see I can write data in my table… when i solve it i will try to sent values from my plc to ignition and try to reed it and write it in the db in mysql and also visualize this table in my ignition hmi…

the error igniton show to me is…

I hope you can help me please

Check in your project properties to make sure that the default database is set. It will be shown similar to the screenshot below. If that doesn’t work, confirm that the database is connected at the gateway level.

2 Likes

My default database was “None”, and when I change this value to my db the problem were solved… thank you!!

mmm now I have an unexpected issue… i imagined that i could obtain the value of a plc tag and write it like this…

model="_CompactLogix L24ER-QB1B_/Program:MainProgram/tableResults/Model";
system.db.runPrepUpdate("INSERT INTO visiontable (Modelo) VALUES (?)",[model]);

but now it shows me another error…

the plc tag value is 3, i want to write this value into the table

You need to use system.tag.read to get the actual value of that tag, or if that’s a direct OPC item path, system.opc.readValue; ex:

model = system.tag.read("_CompactLogix L24ER-QB1B_/Program:MainProgram/tableResults/Model").value
system.db.runPrepUpdate("INSERT INTO visiontable (Modelo) VALUES (?)", [model])
2 Likes

hello, it totally solved my problem… and also i noticed that there is a similar way to do it… instead of write this:

model = system.tag......./Model").value
system.db.runPrepUpdate("INSERT INTO visiontable (Modelo) VALUES (?)", [model])

I can change the “.value” to the runPrepUpdate this way…

model = system.tag.read("_CompactLogix L24ER-QB1B_/Program:MainProgram/tableResults/Model");
system.db.runPrepUpdate("INSERT INTO visiontable (Modelo) VALUES (?)", [model.value])

maybe a lot of peope already know that but i wanted to write this in case someone else dont.

thank you!!

2 Likes

I’m here with a new issue :smile: now when I’m trying to use an event tag when a plc tag change its value got this error…

first the script

and the error…

please help me again :cry:

You haven’t specified a database connection. There’s no default database in tag event scope.

1 Like

Hi pturmel.

I’m having the same problem and I was thinking that it is a problem with the database connection. Can you please explain how do I specify the database connection?

Regards

I’m trying using this code into a tag event to update a table into a database called KPV but it doesn’t work. I’m not sure if the syntax is ok. Any commento or advice is welcome.

Best regards.

if alarmName == "Alarm_1_DryRoom_HighTemp_SL":
		query = "UPDATE FINAL_DRY SET %s = ? WHERE Status = ?" % (TempHum_Impact)
		args = ['1', 'On going']
		database = "KPV"
	    system.db.runPrepUpdate(query, [args], database) 

Finally found the solution. It was a syntax mistake. Thanks for your help.

if alarmName == "Alarm_1_DryRoom_HighTemp_SL":
		query = "UPDATE FINAL_DRY SET TempHum_Impact = ? WHERE Status = ?" 
		args = ['0', 'On going']
		database = "KPV"
		system.db.runPrepUpdate(query, args, database) 
1 Like