# Sorting a table or sorting incoming data?

**URL:** <https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011>\
**Category:** Ignition\
**Tags:** scripting, perspective\
**Created:** [May 28, 2024, 2:31pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011 "2024-05-28T14:31:19Z")\
**Posts on this page:** 20\
**Page:** 2

<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:** [June 3, 2024, 4:01pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/21 "2024-06-03T16:01:21Z")

</div>

> [@The\_Bit\_Plumber](#):
>
> Would I put this in the same table as raw\_data

- You would need to add a column to raw\_data to hold the status of whether a row has been processed or not. My example usues _ack_.
- The results of the parsing would go into a different table. My example uses _parsed\_data_, as that is the one you have mentioned.
- You would need to get the indexes of the rows that were selected. My example uses myIndexColumn, as I don't know what the actual column is or where it appears in the column order.
- Use the list of indexes to update the ack value to 1.
- I recommend putting this as a project script, then call it from a [Gateway Timer Script](https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-in-ignition/gateway-event-scripts#timer-script).

Example project script:

```python
import re

def processSectorData():
	db = 'MyDBConnection'
	limit = 50

	query = "SELECT * FROM raw_data WHERE ack != 1 LIMIT ?"
	dataIn = system.db.runPrepQuery(query, [limit], db)

	if len(dataIn) > 0:

		ndxList = dataIn.getColumnAsList(0)

		dataOut = []

		for row in dataIn:
			message = row['Message']
			for item in message.split('\n\n'):
				newRow = re.split(' was identified| at ', item)
				
				newRow[0] = int(newRow[0].split(' ')[-1])
				
				sector = newRow[1].split(' ')[-1]
				if sector == 'sector':
					newRow[1] = 'N/A'
				else:
					newRow[1]=sector
				newRow[2] = system.date.parse(newRow[2], 'h:mm a, MMM DD, yyyy')
			dataOut.append(newRow)

		qmarks = ('(?,?,?),' * limit)[:-1]
		query = 'INSERT INTO parsed_data (tag_id, sector, time_stamp) VALUES {}'.format(qmarks)
		x = system.db.runPrepUpdate(query, dataOut, myDBConnection)
		
		if x > 0:
			indexList = dataIn.getColumnAsList(0)
			query = 'UPDATE raw_data SET ack = 1 where myIndexColumn IN ({})'.format(','.join(['?']*limit))
			system.db.runPrepUpdate(query, indexList, myDBConnection)

```

Example Gateway Timer Script:

```python
project.processSectorData()

```

---

<div class="post-metadata">

**Author:** ![The\_Bit\_Plumber](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/the_bit_plumber/32/58166_2.png) [@The\_Bit\_Plumber](https://forum.inductiveautomation.com/u/The_Bit_Plumber)\
**Post date:** [June 3, 2024, 9:13pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/22 "2024-06-03T21:13:39Z")

</div>

OK, so I added the columns you said but I something isn't working. It's just returning null values for everything.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/8/086c88eccf0c822ff4ba27b202d556d576d3a335.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/9/293772fcd8ea5fc0f512ec82bf1651120a1f0138.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/0/906a85d593095d3585d0750ddd24ee436a6786f8.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/6/8610e2723c8d03c4e7bcac21617e8be18b61406b.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:** [June 3, 2024, 9:14pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/23 "2024-06-03T21:14:59Z")

</div>

Post code, man! Post code - not pictures of code. Post a screengrab as well if context is important. See [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).

---

<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:** [June 3, 2024, 9:17pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/24 "2024-06-03T21:17:11Z")

</div>

> [@The\_Bit\_Plumber](#):
>
> something isn't working

To add:

_What_ isn't working? What have you tried as troubleshooting?  
For instance, are you sure your script is firing? If you add logging (`system.util.getLogger("myLogger").info("script firing")`) -- is it actually executing?  
Are _any_ rows being added in any database tables? Are there any errors in the gateway logs?

---

<div class="post-metadata">

**Author:** ![The\_Bit\_Plumber](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/the_bit_plumber/32/58166_2.png) [@The\_Bit\_Plumber](https://forum.inductiveautomation.com/u/The_Bit_Plumber)\
**Post date:** [June 3, 2024, 9:25pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/25 "2024-06-03T21:25:03Z")

</div>

The code I am using is unaltered from the post above mine, I just copied and pasted it to test. And I did post screen grabs, or maybe I'm misunderstanding what you mean by screengrab.

Here's the code, Edit: fixed the 'mydbconnection' part

```python
import re

def processSectorData():
	db = 'ATLAS_DB'
	limit = 50

	query = "SELECT * FROM raw_data WHERE ack != 1 LIMIT ?"
	dataIn = system.db.runPrepQuery(query, [limit], db)

	if len(dataIn) > 0:

		ndxList = dataIn.getColumnAsList(0)

		dataOut = []

		for row in dataIn:
			message = row('Message')
			for item in message.split('\n\n'):
				newRow = re.split(' was identified| at ', item)
				
				newRow[0] = int(newRow[0].split(' ')[-1])
				
				sector = newRow[1].split(' ')[-1]
				if sector == 'sector':
					newRow[1] = 'N/A'
				else:
					newRow[1]=sector
				newRow[2] = system.date.parse(newRow[2], 'h:mm a, MMM DD, yyyy')
			dataOut.append(newRow)

		qmarks = ('(?,?,?),' * limit)[:-1]
		query = 'INSERT INTO parsed_data (tag_id, sector, time_stamp) VALUES {}'.format(qmarks)
		x = system.db.runPrepUpdate(query, dataOut, myDBConnection)
		
		if x > 0:
			indexList = dataIn.getColumnAsList(0)
			query = 'UPDATE raw_data SET ack = 1 where myIndexColumn IN ({})'.format(','.join(['?']*limit))
			system.db.runPrepUpdate(query, indexList, myDBConnection)

```

---

<div class="post-metadata">

**Author:** ![The\_Bit\_Plumber](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/the_bit_plumber/32/58166_2.png) [@The\_Bit\_Plumber](https://forum.inductiveautomation.com/u/The_Bit_Plumber)\
**Post date:** [June 3, 2024, 9:27pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/26 "2024-06-03T21:27:09Z")

</div>

The script that is supposed to parse the data from the raw\_data isn't working. It is executing because it is adding rows to the parsed\_data table as shown in the screen shots, except that all the values of the rows are "null".

Edit: I forgot about checking the logs. There were some errors, in spelling and variable naming. It is working without errors now, but it is still not doing what it needs too. All table values are still 'null'.

---

<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:** [June 3, 2024, 9:33pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/27 "2024-06-03T21:33:04Z")

</div>

Add a print or logger for `dataOut` right before this line:

```python
		x = system.db.runPrepUpdate(query, dataOut, myDBConnection)

```

---

<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:** [June 4, 2024, 2:29am UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/28 "2024-06-04T02:29:23Z")

</div>

I don’t think that

```python
row('message')

```

is valid syntax, I see it’s in @JordanCClark’s example, but I’m fairly sure it should be square brackets.

---

<div class="post-metadata">

**Author:** ![The\_Bit\_Plumber](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/the_bit_plumber/32/58166_2.png) [@The\_Bit\_Plumber](https://forum.inductiveautomation.com/u/The_Bit_Plumber)\
**Post date:** [June 4, 2024, 12:45pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/29 "2024-06-04T12:45:00Z")

</div>

Ok I did that but do not see the logs in the Gateway Scripts page. Here's what I added...

```python

		qmarks = ('(?,?,?),' * limit)[:-1]
		query = 'INSERT INTO parsed_data (tag_id, sector, time_stamp) VALUES {}'.format(qmarks)
		logger = system.util.getLogger(myLogger)
		logger.info("Hello")
		x = system.db.runPrepUpdate(query, dataOut, ATLAS_DB)
		
		if x > 0:
			indexList = dataIn.getColumnAsList(0)
			query = 'UPDATE raw_data SET ack = 1 where myIndexColumn IN ({})'.format(','.join(['?']*limit))
			system.db.runPrepUpdate(query, indexList, ATLAS_DB)

```

---

<div class="post-metadata">

**Author:** ![Francisco.Mejia](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/francisco.mejia/32/63246_2.png) [@Francisco.Mejia](https://forum.inductiveautomation.com/u/Francisco.Mejia)\
**Post date:** [June 4, 2024, 1:04pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/30 "2024-06-04T13:04:30Z")

</div>

I haven't seen where you get the value 'purple" is this a different table?

---

<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:** [June 4, 2024, 1:08pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/31 "2024-06-04T13:08:10Z")

</div>

You need to add the ack column to the table where your raw data actually is. I put _raw\_data_ in the script, because that was the table you mentioned earlier.

I don't know your data, or db configurations, so you will need to make it fit.

---

<div class="post-metadata">

**Author:** ![Francisco.Mejia](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/francisco.mejia/32/63246_2.png) [@Francisco.Mejia](https://forum.inductiveautomation.com/u/Francisco.Mejia)\
**Post date:** [June 4, 2024, 1:15pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/32 "2024-06-04T13:15:14Z")

</div>

This will work if you only have a few rows of 'raw data', The more frequent the message is received and the number of tags is big, then you have to worry about affecting the performance of the client.

---

<div class="post-metadata">

**Author:** ![The\_Bit\_Plumber](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/the_bit_plumber/32/58166_2.png) [@The\_Bit\_Plumber](https://forum.inductiveautomation.com/u/The_Bit_Plumber)\
**Post date:** [June 4, 2024, 1:23pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/33 "2024-06-04T13:23:49Z")

</div>

I did, the columns in the raw\_data table are: myindexcolumn, ack, and message.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/9/f9d377c605525e612f3509d854bf938296adbe86.png)

@Francisco.Mejia I'm afraid I don't know what you mean by purple?

@lrose I changed the parathesis to square brackets and it did not work.

---

<div class="post-metadata">

**Author:** ![Francisco.Mejia](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/francisco.mejia/32/63246_2.png) [@Francisco.Mejia](https://forum.inductiveautomation.com/u/Francisco.Mejia)\
**Post date:** [June 4, 2024, 1:29pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/35 "2024-06-04T13:29:40Z")

</div>

Tag ID: 2 was identified in _ **sector purple** _ at 10:24 AM, May 29, 2024

---

<div class="post-metadata">

**Author:** ![The\_Bit\_Plumber](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/the_bit_plumber/32/58166_2.png) [@The\_Bit\_Plumber](https://forum.inductiveautomation.com/u/The_Bit_Plumber)\
**Post date:** [June 4, 2024, 1:31pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/36 "2024-06-04T13:31:30Z")

</div>

> Tag ID: 2 was identified in _ **sector purple** _ at 10:24 AM, May 29, 2024

That is the raw coming in from the device, that is not a table or anything. That is just how the data is formatted.

---

<div class="post-metadata">

**Author:** ![keithms](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/keithms/32/58243_2.png) [@keithms](https://forum.inductiveautomation.com/u/keithms)\
**Post date:** [June 4, 2024, 1:41pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/37 "2024-06-04T13:41:45Z")

</div>

In defense of SQLite, it **is** a fantastic in-process database engine and a perfectly acceptable, even preferred, solution for applications that don't require high concurrency or the ability to handle very large amounts of data. It excels in the use cases it is designed for.

Specifically I like SQLite a lot for single user applications because:

- I don't need to worry about a DB server process
- The DB file format is stable and portable.
- Queries don't rely on a mediating network connection.

That being said, I wouldn't recommend using SQLite for Ignition in most (but not necessarily all) cases.

---

<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:** [June 4, 2024, 1:46pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/38 "2024-06-04T13:46:46Z")

</div>

> [@The\_Bit\_Plumber](#):
>
> `logger = system.util.getLogger(myLogger)`

Have you defined `myLogger` further up in the script? If not then this wont work.

You'll notice that in @paul-griffith's example there are `"` around myLogger.

```python
logger = system.util.getLogger("myLogger")

```

The only way I can see that rows are entered as null, is if dataOut is empty (doesn't seem likely I would expect an error, not a null entry) either way, do all of the messages have two newLines in them? `for item in message.split('\n\n')`

---

<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:** [June 4, 2024, 1:48pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/39 "2024-06-04T13:48:45Z")

</div>

> [@keithms](#):
>
> It excels in the use cases it is designed for.

Concur.

> [@keithms](#):
>
> don't require high concurrency or the ability to handle very large amounts of data

Exactly. Push it hard, it falls over. SCADA applications, even if started small, tend to grow dramatically once end-users discover how valuable the data is. I **strongly** recommend against **ever** using SQLite in Ignition.

---

<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:** [June 4, 2024, 1:50pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/40 "2024-06-04T13:50:35Z")

</div>

Agreed, but its lack of DateTime data types is likely to bite at some stage.

---

<div class="post-metadata">

**Author:** ![Francisco.Mejia](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/francisco.mejia/32/63246_2.png) [@Francisco.Mejia](https://forum.inductiveautomation.com/u/Francisco.Mejia)\
**Post date:** [June 4, 2024, 1:50pm UTC](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011/41 "2024-06-04T13:50:38Z")

</div>

What is the largest SQLite database you have worked with? Does it slow down?

[Previous page](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011.md?page=1)

[Next page](https://forum.inductiveautomation.com/t/sorting-a-table-or-sorting-incoming-data/91011.md?page=3)
