# system.db.runPrepUpdate not working for update query, but working for insert query

**URL:** <https://forum.inductiveautomation.com/t/system-db-runprepupdate-not-working-for-update-query-but-working-for-insert-query/90856>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [May 24, 2024, 9:54am UTC](https://forum.inductiveautomation.com/t/system-db-runprepupdate-not-working-for-update-query-but-working-for-insert-query/90856 "2024-05-24T09:54:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![srinivasan\_s](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@srinivasan\_s](https://forum.inductiveautomation.com/u/srinivasan_s)\
**Post date:** [May 24, 2024, 9:54am UTC](https://forum.inductiveautomation.com/t/system-db-runprepupdate-not-working-for-update-query-but-working-for-insert-query/90856/1 "2024-05-24T09:54:31Z")

</div>

I am using below script which is bind to button component. If button clicked, update query is not working.Just for checking I tried insert, that was working fine! Below is my code.

```python
i=self.view.params.row_id
system.db.runPrepUpdate("INSERT INTO drawing_master (name, drawingno, product) VALUES (?,?,?)", [self.parent.parent.getChild("FlexContainer_0").getChild("Name_text").props.text, self.parent.parent.getChild("FlexContainer_0").getChild("drawing_no_text").props.text, self.parent.parent.getChild("FlexContainer_1").getChild("TextField").props.text])
system.db.runPrepUpdate("UPDATE drawing_master SET name = ?, drawingno = ?,product = ? WHERE id = ?",[self.parent.parent.getChild("FlexContainer_0").getChild("Name_text").props.text,self.parent.parent.getChild("FlexContainer_0").getChild("drawing_no_text").props.text,self.parent.parent.getChild("FlexContainer_1").getChild("TextField").props.text,i])

```

Table data is not getting updated. My DB is MYSQL. Kindly help me to resolve this. I was stuck.

---

<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:** [May 24, 2024, 10:09am UTC](https://forum.inductiveautomation.com/t/system-db-runprepupdate-not-working-for-update-query-but-working-for-insert-query/90856/2 "2024-05-24T10:09:17Z")

</div>

> [@srinivasan\_s](#):
>
> MYSQL

> [@srinivasan\_s](#):
>
> `WHERE id = ?`

Can you verify that 'id' is designated in the database as a primary key constraint?

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [May 24, 2024, 10:22am UTC](https://forum.inductiveautomation.com/t/system-db-runprepupdate-not-working-for-update-query-but-working-for-insert-query/90856/3 "2024-05-24T10:22:35Z")

</div>

Are you getting any errors in the gateway log?

Can you share the table definition for this table?

---

<div class="post-metadata">

**Author:** ![srinivasan\_s](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@srinivasan\_s](https://forum.inductiveautomation.com/u/srinivasan_s)\
**Post date:** [May 24, 2024, 11:00am UTC](https://forum.inductiveautomation.com/t/system-db-runprepupdate-not-working-for-update-query-but-working-for-insert-query/90856/4 "2024-05-24T11:00:05Z")

</div>

yes It was primary key in my table.also autoincrement

---

<div class="post-metadata">

**Author:** ![srinivasan\_s](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@srinivasan\_s](https://forum.inductiveautomation.com/u/srinivasan_s)\
**Post date:** [May 24, 2024, 11:02am UTC](https://forum.inductiveautomation.com/t/system-db-runprepupdate-not-working-for-update-query-but-working-for-insert-query/90856/5 "2024-05-24T11:02:19Z")

</div>

There is no error i am facing!  
Table Definition:  
"CREATE TABLE drawing\_master (  
id INT AUTO\_INCREMENT PRIMARY KEY,  
name VARCHAR(50) NOT NULL,  
drawingno VARCHAR(50) NOT NULL,  
product VARCHAR(100) NOT NULL  
);"

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [May 24, 2024, 11:05am UTC](https://forum.inductiveautomation.com/t/system-db-runprepupdate-not-working-for-update-query-but-working-for-insert-query/90856/6 "2024-05-24T11:05:10Z")

</div>

So no error in the gateway logs?

Is any of the data NULL? You may want to print out all your data to the web browser console using system.perspective.print and verify that you don't have any NULL data that you are trying to update.

Also.. if you can insert but not update with the data, it might be that this

```python
i=self.view.params.row_id

```

isn't returning the data you think it is. Inspect that data as well in the console.

Side note... rather than using the relative paths, you may want to have your components be bound to custom properties and use those instead. Just in case you decide to move things.

---

<div class="post-metadata">

**Author:** ![srinivasan\_s](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@srinivasan\_s](https://forum.inductiveautomation.com/u/srinivasan_s)\
**Post date:** [May 24, 2024, 11:24am UTC](https://forum.inductiveautomation.com/t/system-db-runprepupdate-not-working-for-update-query-but-working-for-insert-query/90856/7 "2024-05-24T11:24:40Z")

</div>

Thank you for your input. Now it is working.  
Corrected Code:  
system.db.runPrepUpdate("UPDATE drawing\_master SET name = ?, drawingno = ?,product = ? WHERE id = ?",[self.parent.parent.getChild("FlexContainer\_0").getChild("Name\_text").props.text,self.parent.parent.getChild("FlexContainer\_0").getChild("drawing\_no\_text").props.text,self.parent.parent.getChild("FlexContainer\_1").getChild("TextField").props.text,self.view.params.row\_id])

---

<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:** [May 24, 2024, 12:45pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepupdate-not-working-for-update-query-but-working-for-insert-query/90856/8 "2024-05-24T12:45:25Z")

</div>

If you follow the scheme I explained in answer to your other question, [Need to execute the script when it entered the perspective page - #2 by Transistor](https://forum.inductiveautomation.com/t/need-to-execute-the-script-when-it-entered-the-perspective-page/90852/2), your corrected code could be written as,

```python
query = """UPDATE drawing_master 
           SET name = ?, drawingno = ?,product = ? 
           WHERE id = ?"""
values = [self.custom.name,
		  self.custom.drawingno,
		  self.custom.product,
		  self.view.params.row_id
         ]
system.db.runPrepUpdate(query, values)

```

Which would be more readable and more robust?

---

<div class="post-metadata">

**Author:** ![srinivasan\_s](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@srinivasan\_s](https://forum.inductiveautomation.com/u/srinivasan_s)\
**Post date:** [May 27, 2024, 5:06am UTC](https://forum.inductiveautomation.com/t/system-db-runprepupdate-not-working-for-update-query-but-working-for-insert-query/90856/9 "2024-05-27T05:06:26Z")

</div>

Hey Transistor,

I felt this is more readable and robust one.
