# Node red and relational database (mysql)?

**URL:** <https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874>\
**Category:** General\
**Created:** [16 January 2019 05:40 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874 "2019-01-16T05:40:38Z")\
**Posts on this page:** 19\
**Page:** 3

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [31 January 2019 20:03 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/41 "2019-01-31T20:03:30Z")

</div>

Thanks for your help, but there is only one device that sends the mqtt topics (tamb, tcorp ...).

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [31 January 2019 20:22 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/42 "2019-01-31T20:22:59Z")

</div>

@Tefita

It is a very simple question that you have not answered.

When a message arrives on an MQTT topic, how does your flow know what patient it is for?

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [31 January 2019 20:27 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/43 "2019-01-31T20:27:46Z")

</div>

The flow does not know that, I just enter the patient's data manually so that it is stored in the "patients" table, and the CEDULA attribute is in the "data" relational table.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [31 January 2019 20:28 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/44 "2019-01-31T20:28:56Z")

</div>

Ok - so at this point I don't know if you are still asking a question here.

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [31 January 2019 20:33 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/45 "2019-01-31T20:33:11Z")

</div>

Thanks for answering what I really want is that the attribute "CEDULA" continue to be stored with the data that comes from the topics but this only happens once. Is it necessary for me to open a new forum debate?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [31 January 2019 20:37 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/46 "2019-01-31T20:37:21Z")

</div>

Please do not open another topic.

> [@Tefita](#):
>
> I really want is that the attribute "CEDULA" continue to be stored with the data that comes from the topics but this only happens once.

I'm not really clear on what you mean.

With your current flow, what do you get in the database? Can you show us an example of the data? And then can you show up what you would _like_ the data to look like.

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [31 January 2019 20:57 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/47 "2019-01-31T20:57:19Z")

</div>

I get these results in the Database (Patients table and data table):

 ![bd2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b4d8d6138969c7dfdc9e7ec232dd9722830bd812.png)

I add the debug messages of each mysql node. ![bd3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d667b441197ff00ba249c13b36ce5c88f8ac261d.png) ![bd4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/298db379522cd24451f3b2bb4c7687b532ba6f49.png)

But later the device keeps sending topics and I look for these to be stored continuously, but they are only stored once because I enter the attribute "CEDULA" only once.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [31 January 2019 21:00 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/48 "2019-01-31T21:00:33Z")

</div>

> [@Tefita](#):
>
> But later the device keeps sending topics and I look for these to be stored continuously, but they are only stored once because I enter the attribute "CEDULA" only once.

Do you mean you want to _update_ the existing row in the datos table where cedula=10002000 with the new data values coming from mqtt?

Or do you want to insert a new row each time a new set of data arrives?

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [31 January 2019 21:15 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/49 "2019-01-31T21:15:44Z")

</div>

Thank you for your answers: I am looking for continuously updating rows Tamb, Tcorp .... Ptcorp every time a new data arrives. But the data in the row "CEDULA" is kept and stored together with the other data, and this is updated only when a different attribute "CEDULA" is entered.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [31 January 2019 21:18 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/50 "2019-01-31T21:18:35Z")

</div>

Just to be absolutely clear... you want **one** row for a given CEDULA value and you want that **one** row to update with the latest values? When a new CEDULA is entered, then a new row is added - or, if one already exists for that CEDULA, that row will be updated.

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [31 January 2019 21:34 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/51 "2019-01-31T21:34:20Z")

</div>

Answering the questions

1. In the table "data" there is a row with the attribute CEDULA, and there are other rows for the data (Tamb, tcorp ...)
2. The CEDULA row already exists and it must be updated when a new "CEDULA" data is entered.

---

<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:** [31 January 2019 22:43 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/52 "2019-01-31T22:43:42Z")

</div>

@Tefita Can you give us a real world walk thru of what happens. Something like:

1. a patient arrives in the office
2. we enter his id (CEDULA??) into the system
3. we then attach a bunch of sensors to him and the sensors start sending MTTQ msgs with data.
4. when the test is done the patient leaves and a new patient enters and the process starts again.

That might give us an idea of what needs to be done becasue at this point, I'm confused.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [31 January 2019 22:48 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/53 "2019-01-31T22:48:40Z")

</div>

It helps when you write down the sequence of events you want to happen. As @zenofmud suggests, a description of how this will get used, one step at a time, would help.

From what I think I understand, does the following sequence make sense?

1. a new CEDULA is entered by the user

2. an mqtt message arrives

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [31 January 2019 23:04 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/54 "2019-01-31T23:04:11Z")

</div>

Thank you very much for answering @zenofmud, and that's what I'm looking for.

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [31 January 2019 23:05 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/55 "2019-01-31T23:05:47Z")

</div>

Thanks also @knolleary

1. If there is a row "CEDULA" in the "patients" table.

2. 

2.1 I get the attribute "CEDULA", but only once.  
2.2 How to generate the appropriate UPDATE sql statement to update that row with this piece of mqtt data ?

Could you please help me

---

<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:** [1 February 2019 02:07 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/56 "2019-02-01T02:07:46Z")

</div>

Have you taken an sql tutorial to see how to construct an update statement? Try googling ‘mysql update tutorial’

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [5 February 2019 05:47 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/57 "2019-02-05T05:47:58Z")

</div>

Thanks for the help, but I have reviewed the mentioned information and I have added the following in the node function "funcion almacenar":

```auto
msg.topic ="INSERT INTO datos (Tamb, Tcorp, rcard, Dcaid, Acorp, Aritm, Ccaid, Ccorp, Critm, Pamb, Pcorp, cedula) VALUES ('" + msg.payload.tamb + "','" +msg.payload.tcorp+ "','" +msg.payload.rcard+ "','" +msg.payload.dcaid+ "','" +msg.payload.acorp+ "','" +msg.payload.aritm+ "','" +msg.payload.ccaid+ "','" +msg.payload.ccorp+ "','" +msg.payload.critm+ "','" +msg.payload.pamb+ "','" +msg.payload.pcorp+ "','" +msg.payload.ced+ "') ON DUPLICATE KEY UPDATE cedula='" +msg.payload.ced+ "'";

```

And I only get the one-time insertion of the data. Any suggestions  
regards

---

<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:** [5 February 2019 09:48 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/58 "2019-02-05T09:48:05Z")

</div>

You use an INSERT to add data the first time. If you want to change that data you do an UPDATE

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [7 February 2019 03:50 UTC](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874/59 "2019-02-07T03:50:16Z")

</div>

Thanks for answering @zenofmud

[Previous page](https://discourse.nodered.org/t/node-red-and-relational-database-mysql/6874.md?page=2)
