# Insert Into MySQL table

**URL:** https://discourse.nodered.org/t/insert-into-mysql-table/51698
**Category:** General
**Tags:** database
**Created:** [30 September 2021 02:49 UTC](https://discourse.nodered.org/t/insert-into-mysql-table/51698 "2021-09-30T02:49:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![JayDLL](https://avatars.discourse-cdn.com/v4/letter/j/df705f/32.png) [@JayDLL](https://discourse.nodered.org/u/JayDLL)
#### Post date: [30 September 2021 02:49 UTC](https://discourse.nodered.org/t/insert-into-mysql-table/51698/1 "2021-09-30T02:49:05Z")

</div>

Hello everyone,

Sorry to have to ask, but I am new to node-red and I am pulling my hair out with this one! I know it's not hard to do this, still, I just can't get it to work. I have a really simple flow, the image is below.

Basically I have a Rockwell PLC that is connected to node-red, the PLC data is coming into node-red correctly and it shows up exactly as it should in the log. I also have a valid connection to MySQL, so I believe the problem is with my insert into statement.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4b25a38c55fb36f79b8e70efad8d749289a16bde.png)

Sorry the image below is small, but I can only post two because I'm a newbee! If you click on it and click on it again it'll zoom up where there is much more information that could help.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/b/bb6792bc630fe6f51a61fd6bd0b19729bc95dd48.jpeg)

If anyone can help me I'd gladly appreciate it, I have an important project to finish under a short deadline.

I'd even consider paid services for anyone who can solve this issue for me, sure, I know that it doesn't work that way on these forums, but this is really important for a water district.

And when I know how to do this I'll respond in turn for other users.

Kind regards,  
Jay

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [30 September 2021 05:15 UTC](https://discourse.nodered.org/t/insert-into-mysql-table/51698/2 "2021-09-30T05:15:43Z")

</div>

What is the msg look like from the serial node?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [30 September 2021 08:43 UTC](https://discourse.nodered.org/t/insert-into-mysql-table/51698/3 "2021-09-30T08:43:31Z")

</div>

You have shown an image containing  
`msg.topic = "...."`  
Where have you put that? It looks as if it should be in a function node, but there isn't a function node in your flow.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [30 September 2021 08:46 UTC](https://discourse.nodered.org/t/insert-into-mysql-table/51698/4 "2021-09-30T08:46:11Z")

</div>

First thing you should do is test a 'select' statement against the database to make sure your mysql node settings are correct.

Add an `insert` node and set msg.topic to  
`select count(*) from your_table_name`  
where 'your\_table\_name' is the name of the table in the database.

This way you can see if your node settings are correct.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [30 September 2021 09:42 UTC](https://discourse.nodered.org/t/insert-into-mysql-table/51698/5 "2021-09-30T09:42:36Z")

</div>

Your SQL statement should be passed to the mysql node in msg.topic.

Using the examples at [https://flows.nodered.org/node/node-red-node-mysql](https://flows.nodered.org/node/node-red-node-mysql) you could use one of these formats:

1 The entire query in msg.topic and assuming msg.payload contains just the plc\_value

```auto
msg.topic = "insert into plc_data (plc_tag, plc_value, plc_timestamp) values ('POTW_ph', msg.payload, '2011-01-01 12:00:00')"

```

2 Data in msg.payload as an array

```auto
msg.payload = ["POTW_ph", 101, "2011-01-01 12:00:00"]
msg.topic = "insert into plc_data (plc_tag, plc_value, plc_timestamp) values (?, ?, ?);"

```

3 Data in msg.payload as a JSON object

```auto
msg.payload = {"plc_tag": "POTW_ph", "plc_value": 101, "timestamp": "2011-01-01 12:00:00"}
msg.topic = "insert into plc_data (plc_tag, plc_value, plc_timestamp) values (:plc_tag, :plc_value, :plc_timestamp);"

```

I've always used the first version but now I've read the page I linked above, I could have saved myself a lot of reformatting by using the third. 🥴

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [29 November 2021 09:42 UTC](https://discourse.nodered.org/t/insert-into-mysql-table/51698/6 "2021-11-29T09:42:42Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
