# Invalid data type? Type casting?

**URL:** <https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [September 29, 2022, 1:07am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627 "2022-09-29T01:07:10Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [September 29, 2022, 1:07am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/1 "2022-09-29T01:07:10Z")

</div>

Hello all,

I have a view that collects the data in several components, labels (used to display data), text fields (used for user input). A button event that scripts all the component props into variables, then into a single variable to pass into a named query.  
Query:

```auto
INSERT INTO [Transaction] (LocID, TypeID, Quantity, Comments, ReportedQty, UserName)
VALUES ((SELECT LocID FROM Bin_Item WHERE ItemID = :itemIdent), :transType, :quantity, :commnts, :rptQty, :userName)

```

Button Script:

```auto
def runAction(self, event):
	"""
	This event is fired when the 'action' of the component occurs.

	Arguments:
		self: A reference to the component that is invoking this function.
		event: An empty event object.
	"""
	namedQuery = "textValueSelectTest01"
	
	item = toInt(self.parent.parent.getChild("Spacer_1").getChild("Label_0").props.text)
	qtyLeft = self.parent.parent.getChild("FlexContainer_2").getChild("QtyLeft_1").props.value
	userName = self.parent.parent.getChild("FlexContainer_2").getChild("Name_1").props.text
	comments = self.parent.parent.getChild("FlexContainer_2").getChild("Comments_1").props.text
	qtyTaken = self.parent.parent.getChild("FlexContainer_2").getChild("Taken_1").props.value
	transType = 1

	params1 = {"myValueX":item, "rptQty":qtyLeft, "userName":userName, "commnts":comments, "quantity":qtyTaken, "transType":transType}
	system.perspective.print(params1)

```

Result from print stmt:

```auto
{'quantity': 1L, 'commnts': u'none', 'rptQty': 19L, 'userName': u'mike', 'myValueX': 4L, 'transType': 1}

```

Question: how can I convert the integer values that are retrieved from the labels into actual integers? You notice the ‘L’ after three of the values.  
I ran into this once before but don’t recall how I dealt with it.  
(Perhaps I need to use a numeric text field instead of a label and just style it differently?)  
I did try to use `toInt()`, but apparently that does not work in this scripting environment. I haven’t found a system function to call for this either.

Thanks,  
Mike

---

<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:** [September 29, 2022, 1:20am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/2 "2022-09-29T01:20:51Z")

</div>

In the scripting environment, you will have to use `int()` to covert the datatype.

Example:  
`print int(str(19L).replace("L",""))` produces the desired output of `19`

To do it in the SQL query you will have to use cast or convert to do it. Here is my stack overflow solution to SQL integer casting to guide you: [mysql-round-float-to-nearest-integer](https://stackoverflow.com/questions/73864603/mysql-round-float-to-nearest-integer/73866206#73866206)

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [September 29, 2022, 1:26am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/3 "2022-09-29T01:26:17Z")

</div>

I’ll give that a try, however, there is something tickling the back of my brain on this…  
If I print each variable independently, no ‘L’ is appended, only when I drop them into one variable that is supposed to contain all the parameters for the named query.

---

<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:** [September 29, 2022, 1:33am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/4 "2022-09-29T01:33:03Z")

</div>

> [@mdemaris](#):
>
> independently, no ‘L’ is appended, only when I drop them into one variable

Thats a good question. I'm not sure why it does that.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [September 29, 2022, 1:34am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/5 "2022-09-29T01:34:29Z")

</div>

This does indeed work!

```auto
{'quantity': 1, 'commnts': u'none', 'rptQty': 19, 'userName': u'mike', 'myValueX': 4, 'transType': 1}

```

Next question: what is the ‘u’ mean? And does it have any effect on the query?

---

<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:** [September 29, 2022, 1:36am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/6 "2022-09-29T01:36:24Z")

</div>

It indicates unicode in the print statement. It shouldn’t effect the query.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [September 29, 2022, 1:51am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/7 "2022-09-29T01:51:18Z")

</div>

Well, for some reason the query is not executing. I’ve tested it independently, so I know that it connects and inserts data.

Got any troubleshooting tips on how to see what is or is not going on? Like a ‘pause during execution’ button?

---

<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:** [September 29, 2022, 1:56am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/8 "2022-09-29T01:56:54Z")

</div>

Does the diagnostic console [ctrl+shift+c] offer any information?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 29, 2022, 2:11am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/9 "2022-09-29T02:11:00Z")

</div>

The `L` means the value was interpreted as a long integer instead of a 32-bit integer. Printing something doesn’t always clearly identify what it really is, but python’s `repr` tries to show what it can. Long integers generally correspond (-ish) to `int8` elsewhere in Ignition. Parameters that are explicitly int8 can take both 32-bit integers and long integers.

Note also that when named queries apply the values in a dictionary to the query, they will try to coerce values into the target datatype, reinterpreting strings if necessary. Take a close look at what datatypes you chose for that NQ.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 29, 2022, 2:13am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/10 "2022-09-29T02:13:49Z")

</div>

Do remember that printing atomic python types general results in implicit use of `str()`. But printing dictionaries or sequences usually invokes `repr()` instead.

---

<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:** [September 29, 2022, 2:19am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/11 "2022-09-29T02:19:57Z")

</div>

> [@mdemaris](#):
>
> If I print each variable independently, no ‘L’ is appended, only when I drop them into one variable

> [@pturmel](#):
>
> atomic python types general results in implicit use of `str()`. But printing dictionaries or sequences usually invokes `repr()`

@pturmel  
That makes perfect sense. Thanks for the clarification

---

<div class="post-metadata">

**Author:** ![tmahaf](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tmahaf/32/23492_2.png) [@tmahaf](https://forum.inductiveautomation.com/u/tmahaf)\
**Post date:** [September 29, 2022, 3:39am UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/12 "2022-09-29T03:39:15Z")

</div>

‘u’ means it’s unicode.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [September 29, 2022, 4:08pm UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/13 "2022-09-29T16:08:02Z")

</div>

I had a suspicion `L` meant Long. All integers are Int8 in the NQ.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [September 29, 2022, 4:09pm UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/14 "2022-09-29T16:09:57Z")

</div>

There is nothing except the printed dictionary when the button is clicked.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [September 29, 2022, 4:37pm UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/15 "2022-09-29T16:37:45Z")

</div>

This is the code for the button click event as modified to remove the `L`.

```auto
def runAction(self, event):
	"""
	This event is fired when the 'action' of the component occurs.

	Arguments:
		self: A reference to the component that is invoking this function.
		event: An empty event object.
	"""
	namedQuery = "textValueSelectTest01"
	
	item = int(str(self.parent.parent.getChild("Spacer_1").getChild("Label_0").props.text).replace("L",""))
	qtyLeft = int(str(self.parent.parent.getChild("FlexContainer_2").getChild("QtyLeft_1").props.value).replace("L",""))
	userName = self.parent.parent.getChild("FlexContainer_2").getChild("Name_1").props.text
	comments = self.parent.parent.getChild("FlexContainer_2").getChild("Comments_1").props.text
	qtyTaken = int(str(self.parent.parent.getChild("FlexContainer_2").getChild("Taken_1").props.value).replace("L",""))
	transType = 1
#	system.perspective.print(item)
	params1 = {"myValueX":item, "rptQty":qtyLeft, "userName":userName, "commnts":comments, "quantity":qtyTaken, "transType":transType}
	system.perspective.print(params1)
	system.db.runNamedQuery(namedQuery, params1)
#should be: namedQuery = "insertSignoutData01"
#should be: params1 = {"itemIdent":item, "rptQty":qtyLeft, "userName":userName, "commnts":comments, "quantity":qtyTaken, "transType":transType}

```

Ok, I just realized I had changed the name of the query to test one value at a time!! Oops.  
Now, using the correct query, I do get an error, and that is a result of using another parameter name instead of the correct one in the correct query.

Now… It works! 🙂

Thank you @pturmel for the info on Ignition’s use of the two Python functions here, thank you @justinedwards.jle for your guide and script help. Thank you @tmahaf as well.

Mike

---

<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:** [September 29, 2022, 5:01pm UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/16 "2022-09-29T17:01:32Z")

</div>

The `int(str().replace())` dance really shouldn’t be necessary. Jython likes to return “longs” for various reasons, but they should seamlessly pass in to anything that expects an integer. I suspect that having the correct named query and parameter names is all you need.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [October 3, 2022, 9:43pm UTC](https://forum.inductiveautomation.com/t/invalid-data-type-type-casting/64627/17 "2022-10-03T21:43:21Z")

</div>

Thanks Paul!
