# Error with System.db.runPrepUpdate

**URL:** <https://forum.inductiveautomation.com/t/error-with-system-db-runprepupdate/94450>\
**Category:** Ignition\
**Tags:** database, scripting, perspective\
**Created:** [August 9, 2024, 8:41pm UTC](https://forum.inductiveautomation.com/t/error-with-system-db-runprepupdate/94450 "2024-08-09T20:41:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucas\_Romano](https://avatars.discourse-cdn.com/v4/letter/l/90ced4/32.png) [@Lucas\_Romano](https://forum.inductiveautomation.com/u/Lucas_Romano)\
**Post date:** [August 9, 2024, 8:41pm UTC](https://forum.inductiveautomation.com/t/error-with-system-db-runprepupdate/94450/1 "2024-08-09T20:41:44Z")

</div>

Hi!

I'm trying to insert data into SQL with an event (on mouse click) that runs a simple script:

```python
def runAction(self, event):
	
	ValorDataHora = system.date.now()
	ValorArea = self.getSibling("CoordinateContainer").custom.Area
	ValorMatricula = self.getSibling("CoordinateContainer").custom.Matricula
	ValorPrioridade = self.getSibling("CoordinateContainer").custom.Prioridade
	ValorEtiqueta = self.getSibling("CoordinateContainer").custom.Etiqueta
	ValorSAP = self.getSibling("CoordinateContainer").custom.SAP
	ValorTipoProblema = self.getSibling("CoordinateContainer").custom.TipoProblema
	ValorOutros = self.getSibling("CoordinateContainer").custom.Outros
	ValorDescricao = self.getSibling("CoordinateContainer").custom.DescProblema
	ValorSolucao = self.getSibling("CoordinateContainer").custom.DesSolucao
	ValorPeriodo = self.getSibling("CoordinateContainer").custom.PeridoRealizacao
	
	
	ValorDataHora1 = str(ValorDataHora)
	ValorArea1 = str(ValorArea)
	ValorMatricula1 = str(ValorMatricula)
	ValorPrioridade1 = str(ValorPrioridade)
	ValorEtiqueta1 = str (ValorEtiqueta)
	ValorSAP1 = str (ValorSAP)
	ValorTipoProblema1 = str (ValorTipoProblema)
	ValorOutros1 = str (ValorOutros)
	ValorDescricao1 = str (ValorDescricao)
	ValorSolucao1 = str (ValorSolucao)
	ValorPeriodo1 = str (ValorPeriodo)
	
	query = "INSERT INTO survey ( datahora, area, matricula, prioridade, etiqueta, sap, tipoproblema, outros, descricao, solucao, periodorealizacao) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
	System.db.runPrepUpdate(query,[ValorDataHora1,ValorArea1,ValorMatricula1,ValorPrioridade1,ValorEtiqueta1,ValorSAP1,ValorTipoProblema1,ValorOutros1,ValorDescricao1,ValorSolucao1,ValorPeriodo1],"SQL")

```

But when i press the button with this event i get the following error:

Error running action 'dom.onClick' on LandingPageView/6\_Resumo@D/root/Button: Traceback (most recent call last): File "function:runAction", line 30, in runAction NameError: global name 'System' is not defined

com.inductiveautomation.ignition.common.script.JythonExecException  
Traceback (most recent call last):  
File "function:runAction", line 30, in runAction  
NameError: global name 'System' is not defined

```
caused by org.python.core.PyException

```

Traceback (most recent call last):  
File "function:runAction", line 30, in runAction  
NameError: global name 'System' is not defined

Ignition v8.1.42 (b2024061810)  
Java: Azul Systems, Inc. 17.0.11

i was looking on [Add Data to Database Video at Inductive University](https://inductiveuniversity.com/videos/add-data-to-database/7.7)

Where the same System.db.runPrepUpdate was used but I'm still stuck with this error.

If anyone has any ideas or solutions for this case, i would appreciate!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 9, 2024, 8:50pm UTC](https://forum.inductiveautomation.com/t/error-with-system-db-runprepupdate/94450/2 "2024-08-09T20:50:34Z")

</div>

Capitalization matters.  
`system`, not `System`.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 9, 2024, 9:05pm UTC](https://forum.inductiveautomation.com/t/error-with-system-db-runprepupdate/94450/3 "2024-08-09T21:05:14Z")

</div>

For some other advice...

> [@Lucas\_Romano](#):
>
> ```python
> ValorDataHora1 = str(ValorDataHora)
> 
> ```

Don't do this (specifically). Dates sent to your database should _stay_ dates, and be translated into the appropriate data for your column _by the JDBC driver_. By casting a date time to string _before_ sending it to the database, you are irrevocably losing information.

> [@Lucas\_Romano](#):
>
> ```python
> ValorArea1 = str(ValorArea)
> ValorMatricula1 = str(ValorMatricula)
> ValorPrioridade1 = str(ValorPrioridade)
> ValorEtiqueta1 = str (ValorEtiqueta)
> ValorSAP1 = str (ValorSAP)
> ValorTipoProblema1 = str (ValorTipoProblema)
> ValorOutros1 = str (ValorOutros)
> ValorDescricao1 = str (ValorDescricao)
> ValorSolucao1 = str (ValorSolucao)
> ValorPeriodo1 = str (ValorPeriodo)
> 
> ```

Also, don't do this (generally).  
If you ever find yourself repeating the same operation many times in a row in a programming context... don't.  
You can easily refactor this to operate on a list (e.g with Python's builtin `map` function), but there's more to do before we get there.

> [@Lucas\_Romano](#):
>
> ```python
> ValorArea = self.getSibling("CoordinateContainer").custom.Area
> 
> ```

You're using the root container to keep track of custom properties - this is _great_ - but the repeated reference to a single property can be avoided with a simple rebinding.  
Between that, and some other refactoring, you can rewrite this whole script to reduce duplication and opportunities for typos significantly:

```python
def runAction(self, event):
    custom = self.getSibling("CoordinateContainer").custom
    parameters = [
        system.date.now(), # datahora
        custom.Area,
        custom.Matricula,
        custom.Prioridade,
        custom.Etiqueta,
        custom.SAP,
        custom.TipoProblema,
        custom.Outros,
        custom.DescProblema,
        custom.DesSolucao,
        custom.PeridoRealizacao
    ]
    query = """
    INSERT INTO survey (datahora,
                        area,
                        matricula,
                        prioridade,
                        etiqueta,
                        sap,
                        tipoproblema,
                        outros,
                        descricao,
                        solucao,
                        periodorealizacao)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    """
    system.db.runPrepUpdate(query, parameters, "SQL")

```
