# Question from a noob: Connecting to sqlite 3

**URL:** <https://discourse.nodered.org/t/question-from-a-noob-connecting-to-sqlite-3/4675>\
**Category:** General\
**Created:** [8 November 2018 09:32 UTC](https://discourse.nodered.org/t/question-from-a-noob-connecting-to-sqlite-3/4675 "2018-11-08T09:32:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eclairee](https://avatars.discourse-cdn.com/v4/letter/e/f0a364/32.png) [@Eclairee](https://discourse.nodered.org/u/Eclairee)\
**Post date:** [8 November 2018 09:32 UTC](https://discourse.nodered.org/t/question-from-a-noob-connecting-to-sqlite-3/4675/1 "2018-11-08T09:32:43Z")

</div>

Hi!!  
I'm new using node - red.

My project is basically this one

 ![proyecto](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/75bf811ebf9feec6f40929ca214e3cda765f2a95.png)

So here my question:

1. How can I get the values from ehall and gas in order to send to my DB?
2. how can I get the 10 higher values from the bd? or can i do it there?

I will apreciate it any help

---

<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:** [8 November 2018 11:18 UTC](https://discourse.nodered.org/t/question-from-a-noob-connecting-to-sqlite-3/4675/2 "2018-11-08T11:18:14Z")

</div>

the values will come in at different times, so your function 'Grabar a BD' will not have the data from both when it runs. If you need the data from both, you will have to feed the two `change` nodes into a `join` node and feed it's output into the function node.

Read up about the `join` node to see how it works and/or do some google searches to learn about it.

---

<div class="post-metadata">

**Author:** ![Eclairee](https://avatars.discourse-cdn.com/v4/letter/e/f0a364/32.png) [@Eclairee](https://discourse.nodered.org/u/Eclairee)\
**Post date:** [9 November 2018 01:11 UTC](https://discourse.nodered.org/t/question-from-a-noob-connecting-to-sqlite-3/4675/3 "2018-11-09T01:11:00Z")

</div>

Hi!! ty for your answer..

I try to do your advice:

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

`[{"id":"30d6141b.37ce6c","type":"function","z":"751ee55e.e3ec3c","name":"Grabar a BD","func":"\nvar outp = [];\noutp[0] = {payload:lines[0]};\noutp[1] = {payload:lines[1]};\n\nvar sql= \"\";\nvar d = new Date();\nvar epoch = d.getTime();\nvar device=\"ESP8266\";\nvar outputs = [];\nsq1 = \"INSERT INTO dhtreadings (eHall,gas,fecha,hora,device)\" + \n \"VALUES (\"+outp[1]+\",\"+outp[0]+\",\"+d+\",\"+epoch+\",\"+device\")\";\noutputs.push({topic:sq1});\n\n//Actualizar status con el tiempo \nvar now=new Date();\nvar yyyy = now.getFullYear();\nvar mm=now.getMonth()<9? \"0\" + (now.getMonth()+1): (now.getMonth()+1); //getMonth()\nvar dd=now.getDate()<10? \"0\" + now.getDate() : now.getDate();\nvar hh = now.getMinutes() < 10 ? \"0\" +now.getMinutes() : now.getMinutes();\nvar ss = now.getSeconds()< 10 ? \"0\" + now.getSeconds(): now.getSeconds();\nnode.status({fill:\"blue\",shape:\"ring\",text:\"Última actualización: \"+dd+\".\"+mm+\".\"+yyyy+\" \"+hh+\":\"+ss});\n\n/*var dia=dd+\"/\"+mm+\"/\"+yyyy;\nvar hora=hh+\":\"+ss;\nvar fecha=dia.toString();\nvar hora_s=hora.toString();\n\nsq1 = \"INSERT INTO lecturasensor (eHall,gas,fecha,hora,device)\" + \n \"VALUES (\"+outp[1]+\",\"+outp[0]+\",\"+fecha+\",\"+hora_s+\",\"+device\")\";\noutputs.push({topic:sq1});*/\n\nreturn [outputs];","outputs":1,"noerr":0,"x":950,"y":620,"wires":[["af8ffcd9.fba2e","79c65c31.aa0304"]]}]`

---

<div class="post-metadata">

**Author:** ![Eclairee](https://avatars.discourse-cdn.com/v4/letter/e/f0a364/32.png) [@Eclairee](https://discourse.nodered.org/u/Eclairee)\
**Post date:** [9 November 2018 01:26 UTC](https://discourse.nodered.org/t/question-from-a-noob-connecting-to-sqlite-3/4675/4 "2018-11-09T01:26:53Z")

</div>

Okay I think I got the problem:  
I try to send 2 obj type, but then,how can I get the number on this?

```
var lines = msg.payload.split("\n");
var outp = [];
outp[0] = {payload:lines[0]};
outp[1] = {payload:lines[1]};

var sql= "";
var d = new Date();
var epoch = d.getTime();
var device="ESP8266";
var outputs = [];
var eHall=outp[0].toInt();
var gas=outp[1].toInt();
/*sq1 = "INSERT INTO dhtreadings (eHall,gas,fecha,hora,device)" + 
        "VALUES ("+outp[1]+","+outp[0]+","+d+","+epoch+","+device+")";
outputs.push({topic:sq1});*/
var fecha=d.toString();
var hora=epoch.toString();
sq1 = "INSERT INTO lecturasensor (eHall,gas,fecha,hora,device)" + 
        "VALUES ("+eHall+","+gas+","+fecha+","+hora+","+device+")";
outputs.push({topic:sq1}); indent preformatted text by 4 spaces

```

I get this from union  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f6f764c6bd71ace4dcbb581fdb3b5ff49a0649bb.png)

What I intent to do, is get the first value and put it on eHall, and the second value on gas. Then send it to my sqlite db.

---

<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:** [9 November 2018 02:25 UTC](https://discourse.nodered.org/t/question-from-a-noob-connecting-to-sqlite-3/4675/5 "2018-11-09T02:25:40Z")

</div>

Can you show what is coming out of the debug node connected to the join. Also please read the post in the FAQ which explains how to add your flow to a post, then edit and fix your posted flow

---

<div class="post-metadata">

**Author:** ![Eclairee](https://avatars.discourse-cdn.com/v4/letter/e/f0a364/32.png) [@Eclairee](https://discourse.nodered.org/u/Eclairee)\
**Post date:** [9 November 2018 04:12 UTC](https://discourse.nodered.org/t/question-from-a-noob-connecting-to-sqlite-3/4675/6 "2018-11-09T04:12:20Z")

</div>

Okay I solve it!!  
I put the join like an array and then I can get both values!! YAY

Now I got two problems: I wanted to send the day and the hour on string Format, but when I do that, it's appears a msg that said: Unexpected String.

```
var value =[];
var value=msg.payload;
var eHall=parseInt(value[1]);
var gas=parseInt(value[0]);
var sql= "";
var d = new Date();
var epoch = d.getTime();
var dispositivo="ESP8266";
var outputs = [];

/*sq1 = "INSERT INTO lecturasensor (eHall,gas,fecha,device)" + 
        "VALUES ("+eHall+","+gas+","+tiempo+","+dispositivo")";
outputs.push({topic:sq1});*/

sq1 = "INSERT INTO dhtreadings (eHall,gas)" + 
        "VALUES ("+eHall+","+gas+")";
outputs.push({topic:sq1});

//Actualizar status con el tiempo 
var now=new Date();
var yyyy = now.getFullYear();
var mm=now.getMonth()<9? "0" + (now.getMonth()+1): (now.getMonth()+1); //getMonth()
var dd=now.getDate()<10? "0" + now.getDate() : now.getDate();
var hh = now.getMinutes() < 10 ? "0" +now.getMinutes() : now.getMinutes();
var ss = now.getSeconds()< 10 ? "0" + now.getSeconds(): now.getSeconds();
node.status({fill:"blue",shape:"ring",text:"Última actualización: "+dd+"."+mm+"."+yyyy+""+hh+":"+ss});

return [outputs];
```

---

<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:** [9 November 2018 08:56 UTC](https://discourse.nodered.org/t/question-from-a-noob-connecting-to-sqlite-3/4675/7 "2018-11-09T08:56:31Z")

</div>

> [@Eclairee](#):
>
> msg that said: Unexpected String.

Show us the full message.

---

<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:** [9 November 2018 10:40 UTC](https://discourse.nodered.org/t/question-from-a-noob-connecting-to-sqlite-3/4675/8 "2018-11-09T10:40:45Z")

</div>

Also, please show the code yu used to create the sqlite table
