# Getting Flow variables into SQLite INSERT

**URL:** https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330
**Category:** General
**Tags:** sqlite
**Created:** [28 May 2024 20:17 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330 "2024-05-28T20:17:54Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![jblack](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@jblack](https://discourse.nodered.org/u/jblack)
#### Post date: [28 May 2024 20:17 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/1 "2024-05-28T20:17:54Z")

</div>

I could use a bit of assistance...

I am attempting to get a flow variable into a SQLite database. Here is my current INSERT command:

INSERT INTO ACC1 (Data, Date, Time) VALUES ("123", strftime('%m-%d-%Y', 'now', 'localtime'), strftime('%H:%M:%S', 'now', 'localtime'));

I would like to replace the "123" data with a Flow variable. I have tried numerous formats but haven't succeeded as of yet.

The INSERT command works as it is and the variable which is Flow.StoredTemp can be read as expected from a debug node.

Any help would be greatly appreciated.

Cheers!

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [28 May 2024 20:22 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/2 "2024-05-28T20:22:52Z")

</div>

Welcome to the forums @jblack

Without knowing the Node you are using...  
I would suggest using parameters

```auto
msg.topic = `INSERT INTO ACC1 (Data, Date, Time) VALUES (?, strftime('%m-%d-%Y', 'now', 'localtime'), strftime('%H:%M:%S', 'now', 'localtime'))`
msg.payload = [flow.get('StoredTemp')]
return msg;

```

The array collection will be used to replace the '?' instances

You can get a flow variable with `flow.get('StoredTemp')`

> **[node-red-node-sqlite](https://flows.nodered.org/node/node-red-node-sqlite)**
>
> A sqlite node for Node-RED

---

<div class="post-metadata">

### Author: ![jblack](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@jblack](https://discourse.nodered.org/u/jblack)
#### Post date: [28 May 2024 20:38 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/3 "2024-05-28T20:38:07Z")

</div>

The current node that I’m using is an Inject node that triggers at an interval.

Thank you!!

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [28 May 2024 20:47 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/4 "2024-05-28T20:47:56Z")

</div>

If you wanted to do this from an `inject` node.  
something like below should work.

```auto
[{"id":"cffb4f27ba66a419","type":"inject","z":"2d7bf6e3.84c97a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"INSERT INTO ACC1 (Data, Date, Time) VALUES (?, strftime('%m-%d-%Y', 'now', 'localtime'), strftime('%H:%M:%S', 'now', 'localtime'))","payload":"[$flowContext('StoredTemp')]","payloadType":"jsonata","x":570,"y":640,"wires":[[]]}]

```

 ![Screenshot 2024-05-28 at 21.47.46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ed86c2446b4d93c5863e269778325ee99204bb66.png)

---

<div class="post-metadata">

### Author: ![RedTom](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/redtom/32/88014_2.png) [@RedTom](https://discourse.nodered.org/u/RedTom)
#### Post date: [28 May 2024 20:53 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/5 "2024-05-28T20:53:18Z")

</div>

template literals  
In JavaScript, backticks (`) are used to create template literals.

e.g.

```auto
var device=msg.payload

var newMsg={}
newMsg.query=`SELECT * FROM test WHERE device='${device}' ORDER BY DESC LIMIT 1`
return newMsg;

```

---

<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: [28 May 2024 21:14 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/6 "2024-05-28T21:14:02Z")

</div>

You can use the prepared statement feature of the sqlite node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/0/5062ed3c6f1cbb9f65ddae902d181612d3d64b51.png)

It will expect to find the value of $data in msg.params.  
I use a function node to populate msg.params, like this

```auto
msg.params = {
    '$data': flow.get('StoredTemp') || 'no data'
}
return msg

```

If you need other fields just add them to the named parameters object:

```auto
msg.params = {
    '$data': flow.get('StoredTemp') || 'no data',
    '$location': 'Manhattan'
}
return msg

```

---

<div class="post-metadata">

### Author: ![jblack](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@jblack](https://discourse.nodered.org/u/jblack)
#### Post date: [28 May 2024 21:34 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/7 "2024-05-28T21:34:06Z")

</div>

Thanks Marcus! The inject node works without error, but when I retrieve the data from the database the "data" field "Null". The Date and Time fields do work as anticipated.

---

<div class="post-metadata">

### Author: ![jblack](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@jblack](https://discourse.nodered.org/u/jblack)
#### Post date: [28 May 2024 21:56 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/8 "2024-05-28T21:56:06Z")

</div>

JBudd the prepared statement method worked just fine. Thanks!

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [29 May 2024 06:58 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/9 "2024-05-29T06:58:53Z")

</div>

Ah!

Sorry **?** was incorrect, you just needed to use the name instead **$data**  
But the solution by @jbudd allows for easier maintenance and probably cleaner.

---

<div class="post-metadata">

### Author: ![elbertelena](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/elbertelena/32/91270_2.png) [@elbertelena](https://discourse.nodered.org/u/elbertelena)
#### Post date: [29 May 2024 11:00 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/10 "2024-05-29T11:00:45Z")

</div>

Hey @marcus-j-davies  
Your post is really helpful.

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [29 May 2024 11:04 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/11 "2024-05-29T11:04:00Z")

</div>

Helpful, but has a slight typo 🙈  
don't use ? just name the params

```auto
msg.topic = `INSERT INTO ACC1 (Data, Date, Time) VALUES ($Data, strftime('%m-%d-%Y', 'now', 'localtime'), strftime('%H:%M:%S', 'now', 'localtime'))`
msg.payload = [flow.get('StoredTemp')]
return msg;

```

---

<div class="post-metadata">

### Author: ![elbertelena](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/elbertelena/32/91270_2.png) [@elbertelena](https://discourse.nodered.org/u/elbertelena)
#### Post date: [29 May 2024 11:09 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/12 "2024-05-29T11:09:43Z")

</div>

Okay! Thanks for the suggestion 🥰

---

<div class="post-metadata">

### Author: ![jblack](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@jblack](https://discourse.nodered.org/u/jblack)
#### Post date: [29 May 2024 12:35 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/13 "2024-05-29T12:35:00Z")

</div>

Thanks Marcus!!!

---

<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: [29 May 2024 13:10 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/14 "2024-05-29T13:10:13Z")

</div>

@marcus-j-davies' function node does work.  
It inserts a record with "data" set to the context variable (if it exists)

But there is no connection between $Data in the INSERT statement and the value in msg.payload other than the position of $data in the field list and the position of the value in the payload array.

Consider if the record is enlarged:

```auto
msg.topic = `INSERT INTO ACC1 (Location, Data, Date, Time) VALUES ($LOCATION, $Data, strftime('%m-%d-%Y', 'now', 'localtime'), strftime('%H:%M:%S', 'now', 'localtime'))`
msg.payload = [flow.get('StoredTemp'), 'Manhattan']
return msg;

```

I think it is better to use a method which gives a name in common between the two msg properties.  
And I believe it's best practice security wise to always use a prepared statement, regardless of the SQL database

---

<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: [12 June 2024 13:10 UTC](https://discourse.nodered.org/t/getting-flow-variables-into-sqlite-insert/88330/15 "2024-06-12T13:10:42Z")

</div>

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