# I have some questions relating database

**URL:** <https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323>\
**Category:** General\
**Created:** [15 August 2018 19:41 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323 "2018-08-15T19:41:35Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![eleckits](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@eleckits](https://discourse.nodered.org/u/eleckits)\
**Post date:** [21 August 2018 23:26 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/22 "2018-08-21T23:26:48Z")

</div>

> [@zenofmud](#):
>
> Attach a debug node (displaying the complete msg object ) to the output of the sql node and see what it returns for the two cases. Then you should be able to figure out what you can test in a switch node to achieve the results you want.

can you show me example?

---

<div class="post-metadata">

**Author:** ![eleckits](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@eleckits](https://discourse.nodered.org/u/eleckits)\
**Post date:** [21 August 2018 23:41 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/23 "2018-08-21T23:41:25Z")

</div>

> [@zenofmud](#):
>
> Attach a debug node (displaying the complete msg object )

8/22/2018, 2:39:58 AM[node: e2f3b3a4.fb279](http://80.209.236.113:1880/#)SELECT \* FROM tab WHERE name LIKE '10' : msg : Object

object

\_msgid: "cf4fc734.30b038"

topic: "SELECT \* FROM tab WHERE name LIKE '10'"

payload: array[1]

0: object

ID: 1001

name: "10"

surname: "11"

time: "2018-08-20T23:08:37.000Z"

---

<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:** [21 August 2018 23:57 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/24 "2018-08-21T23:57:39Z")

</div>

(put three back tic's before and after the information you paste in from the debug node)

Ok, hat looks like tthe debug from a select that returns data, now what about the other case?

---

<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:** [22 August 2018 08:20 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/25 "2018-08-22T08:20:31Z")

</div>

> [@eleckits](#):
>
> msg.topic= "SELECT \* FROM tab WHERE name LIKE '11'";

When you say LIKE '11' what exactly are you hoping that will give you?  
Have a look at [SQLite LIKE - Querying Data Based On Pattern Matching](http://www.sqlitetutorial.net/sqlite-like/) to see the usual way LIKE is used.  
If you are looking for name = '11' then normally one would use  
`msg.topic= "SELECT * FROM tab WHERE name = '11'";`

---

<div class="post-metadata">

**Author:** ![eleckits](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@eleckits](https://discourse.nodered.org/u/eleckits)\
**Post date:** [22 August 2018 18:01 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/26 "2018-08-22T18:01:11Z")

</div>

> [@Colin](#):
>
> msg.topic= "SELECT \* FROM tab WHERE name = '11'";

thanks for the information  
but now want to send to debug (yes) if found 11 and if not found 11 want to send (no) to debug  
can you tell me how to do it?

---

<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:** [22 August 2018 18:37 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/27 "2018-08-22T18:37:24Z")

</div>

Once again I will ask you: what do you see in the debug output when you:

1. Find the a row and
2. when you don't don't find the row?

Once you have the debug outputs, ou should be able to see a difference and use a switch node to test for them.

---

<div class="post-metadata">

**Author:** ![eleckits](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@eleckits](https://discourse.nodered.org/u/eleckits)\
**Post date:** [22 August 2018 18:39 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/28 "2018-08-22T18:39:53Z")

</div>

> [@zenofmud](#):
>
> Once again I will ask you: what do you see in the debug output when you:
> 
> 1. Find the a row and
> 2. when you don't don't find the row?
> 
> Once you have the debug outputs, ou should be able to see a difference and use a switch node to test for them.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7950eb266997a9a32a7707be04891eb1393cab0f.png)

---

<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:** [22 August 2018 18:45 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/29 "2018-08-22T18:45:53Z")

</div>

Please go back and read the previous post and answer _all_ the questions

---

<div class="post-metadata">

**Author:** ![eleckits](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@eleckits](https://discourse.nodered.org/u/eleckits)\
**Post date:** [22 August 2018 18:57 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/30 "2018-08-22T18:57:41Z")

</div>

> [@zenofmud](#):
>
> Please go back and read the previous post and answer _all_ the questions

what question you want answer for?  
I sent picture to you in the previous post

---

<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:** [22 August 2018 19:44 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/31 "2018-08-22T19:44:45Z")

</div>

> [@eleckits](#):
>
> what question you want answer for

@zenofmud asked questions which he helpfully labelled 1 and 2. Since you only provided one answer I deduce that you have not answered both questions.

---

<div class="post-metadata">

**Author:** ![eleckits](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@eleckits](https://discourse.nodered.org/u/eleckits)\
**Post date:** [22 August 2018 20:35 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/32 "2018-08-22T20:35:17Z")

</div>

> [@zenofmud](#):
>
> Please go back and read the previous post and answer _all_ the questions

soory, forget to send another picture  
note : debug set to (complete msg object)  
please check both pictures

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d1378e31d84d7672d8a7184bfa3063931425befa.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4bf1f4e0ce23c7fcfd2d34f083192c241a645570.png)

---

<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:** [22 August 2018 20:39 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/33 "2018-08-22T20:39:07Z")

</div>

So now you have to work out what the key difference in payload is so you can use a Switch node to differentiate.

---

<div class="post-metadata">

**Author:** ![eleckits](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@eleckits](https://discourse.nodered.org/u/eleckits)\
**Post date:** [22 August 2018 20:45 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/34 "2018-08-22T20:45:49Z")

</div>

@zenofmud @Colin I do not know how to do it because I am new to use node red it is appreciated if you tell me what is expression can do to send (yes) if the data is exist and and (no) if the data not exist

---

<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:** [22 August 2018 21:58 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/35 "2018-08-22T21:58:25Z")

</div>

What do you see in the debug messages that is different? Look at each piece of the msg.

---

<div class="post-metadata">

**Author:** ![eleckits](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@eleckits](https://discourse.nodered.org/u/eleckits)\
**Post date:** [22 August 2018 23:10 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/36 "2018-08-22T23:10:26Z")

</div>

> [@zenofmud](#):
>
> What do you see in the debug messages that is different? Look at each piece of the msg.

dear, I see the difference but do not know how to do it  
could you please give me example?

---

<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:** [22 August 2018 23:19 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/37 "2018-08-22T23:19:33Z")

</div>

take a look at the 'switch' node and see if that will help you.

---

<div class="post-metadata">

**Author:** ![eleckits](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@eleckits](https://discourse.nodered.org/u/eleckits)\
**Post date:** [22 August 2018 23:38 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/38 "2018-08-22T23:38:39Z")

</div>

@zenofmud

if data exist :

msg.payload : string[74]

"[{"ID":1001,"name":"10","surname":"11","time":"2018-08-20T23:08:37.000Z"}]"

if data not exist then this :

[]

this is the message so what to do next?

---

<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:** [23 August 2018 01:25 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/39 "2018-08-23T01:25:47Z")

</div>

Use a switch node to test what you have found

---

<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:** [23 August 2018 07:35 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/40 "2018-08-23T07:35:36Z")

</div>

The payload isn't a string if the message exists, it is an array of length 1. If there were multiple matching records then there would be one element for each record.  
However, since all you are interested in is whether a record exists or not I think you would be better using the query  
`SELECT count(*) FROM tab WHERE name='16'`  
which will tell you how many matching records there are and you can then test the result for zero or non-zero in a Switch node.

---

<div class="post-metadata">

**Author:** ![eleckits](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@eleckits](https://discourse.nodered.org/u/eleckits)\
**Post date:** [23 August 2018 11:02 UTC](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323/41 "2018-08-23T11:02:37Z")

</div>

I want to get value from mqtt and insert inside variable (check) then  
msg.topic= "SELECT \* FROM tab WHERE name = 'check'";

but it is not working

[Previous page](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323.md?page=1)

[Next page](https://discourse.nodered.org/t/i-have-some-questions-relating-database/2323.md?page=3)
