# Inserting the last row of a pydataset into a database table

**URL:** <https://forum.inductiveautomation.com/t/inserting-the-last-row-of-a-pydataset-into-a-database-table/19203>\
**Category:** Ignition\
**Tags:** ignition79, database, scripting\
**Created:** [August 10, 2018, 7:05pm UTC](https://forum.inductiveautomation.com/t/inserting-the-last-row-of-a-pydataset-into-a-database-table/19203 "2018-08-10T19:05:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![comaife](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@comaife](https://forum.inductiveautomation.com/u/comaife)\
**Post date:** [August 10, 2018, 7:05pm UTC](https://forum.inductiveautomation.com/t/inserting-the-last-row-of-a-pydataset-into-a-database-table/19203/1 "2018-08-10T19:05:17Z")

</div>

Hi:

I want to insert the last row of a pydataset into an existing database. However, my current script adds the entire dataset each time it is executed. What modifications can I make to my script (below) to ensure only the last row of the dataset is inserted into the database table?

**Script:**

```auto
pydata = system.dataset.toPyDataSet(data)
strQuery = "INSERT INTO Results (t_stamp, serial_numbers, avg) VALUES (?,?, ?)"

```

```
for row in pydata:

	t_stamp = row["Time Stamp"]
	serial_numbers = row["Serial Numbers"]
	avg = row["Ave"]
	args = [t_stamp, serial_numbers, avg]
	system.db.runPrepUpdate(strQuery, args)

```

**Additional Information** : To avoid `DUPLICATE KEY` errors, I removed the `PRIMARY KEY` from my `serial_number` column. I would like to have a `PRIMARY KEY` column either with an `AUTO-INCREMENT INDEX` or my `serial_number` column.

Thank you.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 10, 2018, 7:35pm UTC](https://forum.inductiveautomation.com/t/inserting-the-last-row-of-a-pydataset-into-a-database-table/19203/2 "2018-08-10T19:35:51Z")

</div>

Hi! Welcome to the forum. 🙂

The quickest change would be to move the runPrepUpdate out of the loop:

```auto
for row in pydata:
	t_stamp = row["Time Stamp"]
	serial_numbers = row["Serial Numbers"]
	avg = row["Ave"]
	args = [t_stamp, serial_numbers, avg]

system.db.runPrepUpdate(strQuery, args)

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 10, 2018, 7:50pm UTC](https://forum.inductiveautomation.com/t/inserting-the-last-row-of-a-pydataset-into-a-database-table/19203/3 "2018-08-10T19:50:17Z")

</div>

A bit shorter version.

```auto
lastRow = pydata[len(pydata)-1]
t_stamp = lastRow["Time Stamp"]
serial_numbers = lastRow["Serial Numbers"]
avg = lastRow["Ave"]

system.db.runPrepUpdate(strQuery, [t_stamp, serial_numbers, avg])

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 10, 2018, 7:59pm UTC](https://forum.inductiveautomation.com/t/inserting-the-last-row-of-a-pydataset-into-a-database-table/19203/4 "2018-08-10T19:59:16Z")

</div>

Last one. I promise!

To answer your other question that I quite overlooked about adding a primary key column, (I’m gonna assume MySQL, since you didn’t mention what you’re using.)

```auto
ALTER TABLE table_name ADD new_column int NOT NULL AUTO_INCREMENT primary key

```

---

<div class="post-metadata">

**Author:** ![comaife](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@comaife](https://forum.inductiveautomation.com/u/comaife)\
**Post date:** [August 10, 2018, 8:12pm UTC](https://forum.inductiveautomation.com/t/inserting-the-last-row-of-a-pydataset-into-a-database-table/19203/5 "2018-08-10T20:12:27Z")

</div>

Hi!

Thanks for welcoming me to the forum; much appreciated!

That’s exactly what I was looking for! Thanks!
