Read/Write and Clear from an Array Tag (integer array)

Based on this post,

This is how you would read values from an Integer Array Tag:

tagValues = system.tag.readBlocking(tagPath)
arrayTag = tagValues[0].value
for value in arrayTag:
	print value

But how would you write (append) values to the next available element in the Array?
How would you clear all the values from this Array tag (so that I can start adding values starting fromthe fist element)?

v = system.tag.readBlocking("[default]TestArray")[0].value
print v

# append
v_append = [i for i in v]
v_append.append(42)

system.tag.writeBlocking(["[default]TestArray"], [v_append])
print system.tag.readBlocking("[default]TestArray")[0].value

# clear
system.tag.writeBlocking(["[default]TestArray"], [[]])
print system.tag.readBlocking("[default]TestArray")[0].value

# set back to [1,2,3]
system.tag.writeBlocking(["[default]TestArray"], [[1, 2, 3]])
print system.tag.readBlocking("[default]TestArray")[0].value

result:

array(java.lang.Integer, [1, 2, 3])
[Good]
array(java.lang.Integer, [1, 2, 3, 42])
[Good]
array(java.lang.Integer)
[Good]
array(java.lang.Integer, [1, 2, 3])
>>> 
1 Like

Thank you SO MUCH!

Hi Kevin, I have another question.

This is what I am trying to do... In postgresql, is the query I am doing where the Col named Good_Parts takes an integer Array. So in PGAdmin, the following would be the correct sintax :

INSERT INTO oee_history ("Good_Parts")
       VALUES ( '{0,1,2,3,4,0,1,2,3,4,1,2,3,4,5,6}' );

Using the Ignition Script Console, this is also a correct sintax which works for me by reading the integer array type of tag:

v = system.tag.readBlocking("[default]TestArray")[0].value
Col1 = "\"Good_Parts\""
system.db.runPrepUpdate("INSERT INTO oee_history ("+Col1+") VALUES (?);", [v], "DatabaseConnection")

However, when I try to insert to the database an array of integers I created using a list, it does not work... how can I convert the list type to an integer array type?

The following code does not work, how can I make it work with a list in the Script Console?

# I already tried  using the following lists (list1, list2, list3, list4) but none of them work so far
list1 = [0,1,2,3,4,0,1,2,3,4,1,2,3,4,5,6] 
list2 = "\'{0,1,2,3,4,0,1,2,3,4,1,2,3,4,5,6}\'"
list3 = "{0,1,2,3,4,0,1,2,3,4,1,2,3,4,5,6}"
list4 = '{0,1,2,3,4,0,1,2,3,4,1,2,3,4,5,6}'
system.db.runPrepUpdate("INSERT INTO oee_history ("+Col1+") VALUES (?);", [list1], "Historian")

And these are some of the errors I am getting in the script console:

#Error for List1
Caused by: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: Unable to decode arguments

#Error for List2
Caused by: org.postgresql.util.PSQLException: ERROR: column "Good_Parts" is of type integer[] but expression is of type character varying
  Hint: You will need to rewrite or cast the expression.
Caused by: java.io.InvalidClassException: failed to read class descriptor

#Error for List3
Caused by: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: SQL error for "INSERT INTO oee_history ("Good_Parts") VALUES (?);": ERROR: column "Good_Parts" is of type integer[] but expression is of type character varying
  Hint: You will need to rewrite or cast the expression.

#Error for List4
Caused by: org.postgresql.util.PSQLException: ERROR: column "Good_Parts" is of type integer[] but expression is of type character varying
  Hint: You will need to rewrite or cast the expression.

Does it work if you convert the list into a jarray first?

v = [0,1,2,3]
import jarray
from java.lang import Integer
v_arr = jarray.array(v, Integer)

To be honest I'm not even sure if array columns are supported or not, but see if this gets you to an error somewhere further.

1 Like

It does work!! Thank you very much for your great help!!

2 Likes

Ooooo! Passing arrays to PostgreSQL didn't work in the past. I wonder when this changed. But this could be a game-changer for those needing to use the IN (....) clause with a variable list of elements.

1 Like

I was surprised to see it work, because this function isn't using the setArray API, but it seems to work anyway :man_shrugging:

I'm admittedly pretty unfamiliar with this stuff. Maybe it depends on the JDBC driver implementation in some way.

2 Likes