# Insert information on SQL with script editor

**URL:** <https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678>\
**Category:** Ignition\
**Created:** [June 15, 2020, 9:06pm UTC](https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678 "2020-06-15T21:06:07Z")\
**Posts on this page:** 11\
**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:** [June 15, 2020, 9:06pm UTC](https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678/1 "2020-06-15T21:06:07Z")

</div>

Hello

I have three buttons and 2 Display LED, in the button 1 and button 2 I have active the event action performed for sum the event click of button on tag sum event an show it in Display LED. in the button 3 I need the action click for save the informacion of button 1 and button 2

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

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

How could I save this information using script editor?

this is my code currentrly

```auto
fpmi.db.runUpdateQuery("INSERT INTO MyTable (Column1, Column2) VALUES ('Linea_Assy', 'event.source.parent.getComponent('Label 1').text', 'event.source.parent.getComponent('Led Display').dataQuality''')")

```

type or paste code here

```auto

```

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [June 15, 2020, 9:17pm UTC](https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678/2 "2020-06-15T21:17:23Z")

</div>

Assuming 7.9:

```auto
clear = system.tag.read("[default]CLEAR")
decol = system.tag.read("[default]DECOLORACION")
system.db.runPrepUpdate("INSERT INTO db.table_name (column_1_name, column_2_name) VALUES (?,?)", [clear, decol])

```

---

<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:** [June 16, 2020, 3:09pm UTC](https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678/5 "2020-06-16T15:09:05Z")

</div>

hello @Matrix_Engineering ring

I have this INSERT

```auto
INS = "INSERT INTO Defectos (Linea, Qty, Defecto, fecha) VALUES (?, ?, ?, ?)"
crackQty = system.tag.read("Botones/Linea_Assy/CRACK")
FisuraQty = system.tag.read("Botones/Linea_Assy/FISURA")
Fecha = system.date.now()
system.db.runPrepUpdate(INS,['Linea_Assy',crackQty.value,'Crack',Fecha],["Linea_Assy",FisuraQty.value,"Fisura",Fecha],"TOPY_MW")

```

but retur this error

Error executing script for event actionPerformed on component: Button 3

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [June 16, 2020, 3:57pm UTC](https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678/6 "2020-06-16T15:57:45Z")

</div>

```auto
INS = "INSERT INTO Defectos (Linea, Qty, Defecto, fecha) VALUES (?, ?, ?, ?)"
crackQty = system.tag.read("Botones/Linea_Assy/CRACK")
FisuraQty = system.tag.read("Botones/Linea_Assy/FISURA")
Fecha = system.date.now()
system.db.runPrepUpdate(INS,[crackQty, Fecha, FisuraQty, Fecha],"TOPY_MW")

```

You have the formatting wrong, see above as an example still might not be exactly what you need but will hopefully guide you

---

<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:** [June 16, 2020, 4:01pm UTC](https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678/7 "2020-06-16T16:01:24Z")

</div>

But I need the data CrackQty and FisuraQty save on Qty

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [June 16, 2020, 4:02pm UTC](https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678/8 "2020-06-16T16:02:53Z")

</div>

Do you want the sum of them? So if crackQty is 2 and FisuraQty is 3 then qty in DB = 5?

---

<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:** [June 16, 2020, 4:06pm UTC](https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678/9 "2020-06-16T16:06:56Z")

</div>

No, I need to save crackQty and FisuraQty in the Qty database field

in SQL something like this

```auto
INSERT IN DEFECTS (Linea, Qty, Default, date) VALUES ('Linea_Assy', crackQty.value, 'Crack', Date), ('Linea_Assy', FisuraQty.value, 'Fissure', Date)

```

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [June 16, 2020, 4:10pm UTC](https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678/10 "2020-06-16T16:10:20Z")

</div>

You need to run two insert statements then. You can’t write two values to one column.

---

<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:** [June 16, 2020, 4:21pm UTC](https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678/11 "2020-06-16T16:21:27Z")

</div>

I can’t have another solution, something like this

```auto
INS = "INSERT INTO Defectos (Linea, Qty, Defecto, fecha) VALUES (?, ?, ?, ?)"
crackQty = system.tag.read("Botones/Linea_Assy/CRACK")
FisuraQty = system.tag.read("Botones/Linea_Assy/FISURA")
Fecha = system.date.getDate(year, month, day)
VAL = [('Linea_Assy',crackQty.value,'Crack',Fecha),('Linea_Assy',FisuraQty.value,'Fisura',Fecha)]
system.db.runPrepUpdate(INS,VAL,"TOPY_MW")

```

I don’t have so many INSERT

---

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [June 16, 2020, 4:40pm UTC](https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678/12 "2020-06-16T16:40:40Z")

</div>

You could either run the `INSERT` query twice or insert multiple rows at once.

```auto
# Option 1
query = "INSERT INTO Defectos (Linea, Qty, Defecto, fecha) VALUES (?, ?, ?, ?)"
...
# Run each INSERT statement separately
values = ['Linea_Assy', crackQty.value, 'Crack', Fecha]
system.db.runPrepUpdate(query, values )
...
values = ['Linea_Assy', FisuraQty.value, 'Fisura', Fecha]
system.db.runPrepUpdate(query, values )

# Option 2
query = "INSERT INTO Defectos (Linea, Qty, Defecto, fecha) VALUES (?, ?, ?, ?), (?, ?, ?, ?)"
...
values = ['Linea_Assy', crackQty.value, 'Crack', Fecha, 'Linea_Assy', FisuraQty.value, 'Fisura', Fecha]
system.db.runPrepUpdate(query, values )

```

I haven’t tried something like Option 2 in Ignition, but it’s a valid SQL statement. Assuming your DB is MSSQL; allthough a little hard to read.

---

<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:** [June 16, 2020, 4:45pm UTC](https://forum.inductiveautomation.com/t/insert-information-on-sql-with-script-editor/35678/13 "2020-06-16T16:45:11Z")

</div>

thank @cesrom srom

the second option is my solution
