# System db update the data into the latest record

**URL:** <https://forum.inductiveautomation.com/t/system-db-update-the-data-into-the-latest-record/56350>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [February 21, 2022, 3:52am UTC](https://forum.inductiveautomation.com/t/system-db-update-the-data-into-the-latest-record/56350 "2022-02-21T03:52:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![uy\_huynh](https://avatars.discourse-cdn.com/v4/letter/u/54ee81/32.png) [@uy\_huynh](https://forum.inductiveautomation.com/u/uy_huynh)\
**Post date:** [February 21, 2022, 3:52am UTC](https://forum.inductiveautomation.com/t/system-db-update-the-data-into-the-latest-record/56350/1 "2022-02-21T03:52:25Z")

</div>

Hi guys,  
I’m trying to implement a value changed script to update the end\_time to the row that already had start\_time logged. At the moment, I’m able to insert them into 2 separate rows. Please see attached pics and code!

```
start_time= system.date.format(system.date.now(), "yyyy-MM-dd HH:mm:ss")
end_time= system.date.format(system.date.now(), "yyyy-MM-dd HH:mm:ss")

if previousValue.value == False and initialChange == False :
 	system.db.runPrepUpdate("INSERT INTO downtime_data.downtime_debarker (start_time) VALUES (?)",[start_time],"downtime_data")
if currentValue.value == False and initialChange == False :
 	system.db.runPrepUpdate("INSERT INTO downtime_data.downtime_debarker (end_time) VALUES (?)",[end_time],"downtime_data",)

```

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

---

<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:** [February 21, 2022, 8:33am UTC](https://forum.inductiveautomation.com/t/system-db-update-the-data-into-the-latest-record/56350/2 "2022-02-21T08:33:52Z")

</div>

I’m not sure I understand the problem but I think there are two issues:

1. You might be looking for a Python _None_ rather than _False_. (They’re not the same.) Try  
`if previousValue.value is None and initialChange is None:`

2. If you’re trying to update _end\_time_ then use the SQL UPDATE command rather than insert. The raw SQL syntax is,

```auto
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

```

The Ignition script syntax would be like,

```auto
query = ('UPDATE downtime_data.downtime_debarker ' 
		+ 'SET end_time= ? '
		+ 'WHERE start_time = ? ')
args = [end_time, start_time] 
system.db.runPrepUpdate(query, args)

```

What is `“downtime\_data”? You have it quoted so that’s just going to be a string “downtime\_data” rather than the contents of the down\_time variable.

---

<div class="post-metadata">

**Author:** ![uy\_huynh](https://avatars.discourse-cdn.com/v4/letter/u/54ee81/32.png) [@uy\_huynh](https://forum.inductiveautomation.com/u/uy_huynh)\
**Post date:** [February 22, 2022, 8:37pm UTC](https://forum.inductiveautomation.com/t/system-db-update-the-data-into-the-latest-record/56350/3 "2022-02-22T20:37:50Z")

</div>

1. It is a boolean tag. So, I’m just trying to make valued change script from this boolean tag, and skip the quality of the tag which is initialChange.
2. With a bit changes, I’ve made it successfully based on Transistor’s help:

```auto
start_time= system.date.format(system.date.now(), "yyyy-MM-dd HH:mm:ss")
	end_time= system.date.format(system.date.now(), "yyyy-MM-dd HH:mm:ss")
	
	query = ('UPDATE downtime_data.downtime_debarker ' 
			+ 'SET end_time= ? '
			+ 'ORDER BY start_time DESC ' +'LIMIT 1' )
	args = [end_time] 
	
	
	if previousValue.value == False and initialChange == False :
			 system.db.runPrepUpdate("INSERT INTO downtime_data.downtime_debarker (start_time) VALUES (?)",[start_time],"downtime_data")
	if currentValue.value == False and initialChange == False :
			system.db.runPrepUpdate(query, args,"downtime_data")

```

Basically, it will update the end\_time to the latest record of start\_time whenever the boolean tags changes its value.

Thanks for your help Transistor!!!

---

<div class="post-metadata">

**Author:** ![uy\_huynh](https://avatars.discourse-cdn.com/v4/letter/u/54ee81/32.png) [@uy\_huynh](https://forum.inductiveautomation.com/u/uy_huynh)\
**Post date:** [February 22, 2022, 8:40pm UTC](https://forum.inductiveautomation.com/t/system-db-update-the-data-into-the-latest-record/56350/4 "2022-02-22T20:40:19Z")

</div>

“downtime\_data” is my SQL database name. IDK why it only works with this syntax inside. Maybe I have various databases.

---

<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:** [February 22, 2022, 9:16pm UTC](https://forum.inductiveautomation.com/t/system-db-update-the-data-into-the-latest-record/56350/5 "2022-02-22T21:16:25Z")

</div>

Tip: use the `</>` code formatting button to preserve indentation and avail of syntax highlighting. It’s especially important for Python code. Can you fix your answer (pencil icon) to make it readable? Thanks!

---

<div class="post-metadata">

**Author:** ![uy\_huynh](https://avatars.discourse-cdn.com/v4/letter/u/54ee81/32.png) [@uy\_huynh](https://forum.inductiveautomation.com/u/uy_huynh)\
**Post date:** [February 22, 2022, 9:21pm UTC](https://forum.inductiveautomation.com/t/system-db-update-the-data-into-the-latest-record/56350/6 "2022-02-22T21:21:58Z")

</div>

Oops! Learn new things every day. Cheers Transistor!
