# SQL statement inserting record twice

**URL:** <https://forum.inductiveautomation.com/t/sql-statement-inserting-record-twice/77567>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [August 8, 2023, 1:10pm UTC](https://forum.inductiveautomation.com/t/sql-statement-inserting-record-twice/77567 "2023-08-08T13:10:03Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![Tim\_Carlton](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tim_carlton/32/52835_2.png) [@Tim\_Carlton](https://forum.inductiveautomation.com/u/Tim_Carlton)\
**Post date:** [August 8, 2023, 3:07pm UTC](https://forum.inductiveautomation.com/t/sql-statement-inserting-record-twice/77567/21 "2023-08-08T15:07:04Z")

</div>

Then my question is _Why is this code inserting **only** two rows_...

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 8, 2023, 3:08pm UTC](https://forum.inductiveautomation.com/t/sql-statement-inserting-record-twice/77567/22 "2023-08-08T15:08:34Z")

</div>

My guess is that the conditional inside the loop is only true one time other then the first trip.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [August 8, 2023, 3:11pm UTC](https://forum.inductiveautomation.com/t/sql-statement-inserting-record-twice/77567/23 "2023-08-08T15:11:14Z")

</div>

Is it possible for you change your insert query to directly via a named query with parameters ie `INSERT INTO TABLE2 (PART, LOT) VALUES (:part, :lot)`? Your current approach is an indirect to insert and prone to error, all it would take is someone else changing the CHANGE column in that other table to mess up your inserts in unexpected and hard to trace out ways, or just the CHANGE column not being reset properly etc. If you were inserting the parameters directly you could look at your NamedQuery logs if something went wrong, see what is being told to be inserted and you'd be sure that each system.db.runNamedQuery statement was only inserting one row.

Not to mention what of the case if two clients are running at the same time. That could give you a real headache.

---

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [August 8, 2023, 3:19pm UTC](https://forum.inductiveautomation.com/t/sql-statement-inserting-record-twice/77567/24 "2023-08-08T15:19:53Z")

</div>

TABLE1 has 4 columns; BARCODE, LOT, PART, CHANGE. When a row is filled out, change is always 0. but when a new lot and part don't match the previous lot and part, the pervious change is updated to 1 so that's why it's in the loop.

I'll try initializing prevLot and prevPart how you recommended

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 8, 2023, 4:41pm UTC](https://forum.inductiveautomation.com/t/sql-statement-inserting-record-twice/77567/25 "2023-08-08T16:41:17Z")

</div>

> [@lrose](#):
>
> I would suggest `if prevLot is not lot and prePart is not part:` since you're comparing strings.

do NOT compare strings with `is` !  
`is` checks if the objects are the same, not if they're equal. Won't work with strings !

It works with `None`, because it's a singleton, and even with small numbers (because they're also pre-declared as singletons to make things... I don't know, easier ? faster ?).

I'm not sure if this is reliable, but it prints "900", So I'm guessing numbers up to 900 are singletons and can be compared with `is`.

```python
for x, n in enumerate(xrange(3000)):
	if x is not n:
		print x
		break

```

But strings, never ! You can try with variables, with literals, whatever, it just won't work:

```python
"hello" is "hello"

```

> \> False

Unless, obviously, if you want to know if they're the same object.

---

<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 8, 2023, 4:50pm UTC](https://forum.inductiveautomation.com/t/sql-statement-inserting-record-twice/77567/26 "2023-08-08T16:50:31Z")

</div>

> [@pascal.fragnoud](#):
>
> I'm not sure if this is reliable, but it prints "900", So I'm guessing numbers up to 900 are singletons and can be compared with `is`.

Extra fun in a Java environment, where there's a **configurable** boxed Integer cache, iirc. I have no idea whether that affects Jython or not, but I certainly wouldn't want to find out 🙂

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 8, 2023, 4:59pm UTC](https://forum.inductiveautomation.com/t/sql-statement-inserting-record-twice/77567/27 "2023-08-08T16:59:19Z")

</div>

ughh, thanks for keeping me in line. Gee wizz.

[Previous page](https://forum.inductiveautomation.com/t/sql-statement-inserting-record-twice/77567.md?page=1)
