I’m trying to take a bunch of values from a PAC table and write each value as a separate record in a MS SQL database. How do I separate the values or reference them individually? Here’s what I’ve got:
The msg.PlannedHours is obviously an array (I guess). NR data types are a mystery to me. In any case, seems to me I could loop through them and insert them into my SQL table, one at a time, and that would give me my result. However I don’t know how to reference them individually. I’m sure there’s some kind of syntax I’m not aware of.
Dave,
Take a look at the split node.
Just drop it between your groov node and your debug node in this screen shot.
It will take the table and split it into separate payloads.
Also, don’t forget the tip to put the debug node into ‘full message payload’ mode, it can help show all the data, rather than just msg.payload.
Stick with it, I am also at that same place where I am sometimes just not sure about the data types… We have actually spent the past 2 days playing with Node-RED at home, getting lots of cool stuff done, but sometimes just feel disconnected, but when it clicks, its pretty sweet.
Dave, sorry, I don’t fully understand your question… You want to do a select now? (Reference).
Can you give us a screen shot (snip) of the bit you mean?
So I want to either do a SQL INSERT command for the whole array or loop through these values and INSERT them one at a time. If this was Optoscript I would do something like msg.payload[x] to reference each one of my table elements. However, from what I can tell you can’t do that kind of reference in transact-sql. So my question is how do I do this? I see where it can be done by declaring each element but that doesn’t work when you have hundreds of data items. This table I’m referring to is just a sample.
So how about if I move to MySQL? In this post: SNAP PAC and groov Nodes - #24 by Beno it appears you’re inserting an array into a MySQL database. If that works, I’m all for it. I just can’t declare each variable. My whole application is written in arrays, and if I have any hope of being able to do any kind of meaningful reporting of all the data we;re collecting, I need to be able to automate the process of inserting it into a database.
If you are using MSSQL 2008+ you can perform a multi-row insert which looks like this for a single column table: insert into tblData1 (Item) values (15), (12), (10);
If you have multiple columns to insert: insert into tblData1 (Item1, Item2) values (15, 10), (12, 7), (10, 5);
So after your split node, you can place a template node with the following simple template:
({{{payload}}})
This will generate (x) for each item in your array, for example (15)
You then can place a join node to concatenate all these together - set the join node to manual and to create a string joined using a comma.
The payload will now be (15),(12),(10)…etc
Then in your mssql node, place the final insert template:
insert into dbo.tblData1 (Item) values {{{payload}}};
Brother Philip, you are a GENIUS!!! If you only knew how many hours I spent combing the web, installing different platforms, looking at endless lines of complicated JavaScript and SQL queries no mere mortal could comprehend. I was done.
I thought I would share this for anyone who wants to try this with MySQL. Don’t really know how to do any of this but this is what I changed to make it work from what you see above. If someone else has a better way of doing this, please let me know.
First you have to create a template node and change its type to msg.topic to put in your query. You also have to modify the query for the MySQL syntax.
The other thing you have to do is remove the Join node. Otherwise MySQL thinks your inserting a single record with the amount of columns equal to the length of your table (I hope I’m explaining that right). If you don’t you get the error “column count doesn’t match the value count at row 1”.
MySQL should support the same multi-row insert that MSSQL 2008+ does. By removing the join, you are sending and insert command for each row separately which will have a performance penalty - but it will still work so if if performance is okay, you should be fine.
From the looks of it I have three msg.payload of two array elements. So trying to mimic what I did before to make this work I used the template node and put ({{{payload}}}) in it. However when I do that, it turns it into a string which of course doesn’t work:
Since you are sending a varchar, you need to surround each value in quotes/apostrophes. So you will end up with (‘SL2-1-V1’, ‘31’). You should then be able to perform a muli-row insert.
You will need to move each item into its own parameter, since mustache templates don’t support arrays. (Edit: This is wrong, see edit at bottom) So add a function node after the split node and enter the following javascript:
Success!!! Again, I can’t thank you enough! I now I have a template I can modify moving forward. I just changed the Function node to add a third column and it worked fine, You can probably see that I’m using a single column table and turning it into a mutli-dimensional array by grabbing groups of table elements and inserting them as rows into my database. A little klugy but it’s the only way I know to do that in my strategy. I think it was Opto Mary who posted that particular trick and it works great.
Is there any chance you can show what your finished code is like? I have a similar situation going in. I have mysql setup to send multiple values to a database but currently I have to make separate connections to each table meaning I have to have multiple SQL nodes instead of one.
I want to beable to make one large sql statement and send the info to multiple tables through one singular connection.