# Sqlite database help

**URL:** https://discourse.nodered.org/t/sqlite-database-help/97023
**Category:** General
**Tags:** database
**Created:** [12 May 2025 11:33 UTC](https://discourse.nodered.org/t/sqlite-database-help/97023 "2025-05-12T11:33:42Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![fares-31](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fares-31/32/100613_2.png) [@fares-31](https://discourse.nodered.org/u/fares-31)
#### Post date: [12 May 2025 11:33 UTC](https://discourse.nodered.org/t/sqlite-database-help/97023/1 "2025-05-12T11:33:42Z")

</div>

![dd](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/578e3e5df91f2103033234363053e4bc605656eb.png)  
 ![ss](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/544251665d6cc171bcc6a8ffebcf026275355fcd.png)  
 ![ddd](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e92c7d95c6c1657cea36f6536d3798aee2c4a90d.png)  
hy, i need a help in this situation  
I have this topic which sends the voltage values ​​in real time and the type of msg.playload which is sent is "number", I do this function to transform the type to array to insert into the sqlite table but the result is deferred in debug 1

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [12 May 2025 12:15 UTC](https://discourse.nodered.org/t/sqlite-database-help/97023/2 "2025-05-12T12:15:39Z")

</div>

Not sure what sqlite node you are using but the [node-red-node-sqlite (node) - Node-RED](https://flows.nodered.org/node/node-red-node-sqlite) seems to suggest the values should be ($tension).

---

<div class="post-metadata">

### Author: ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)
#### Post date: [12 May 2025 16:47 UTC](https://discourse.nodered.org/t/sqlite-database-help/97023/3 "2025-05-12T16:47:48Z")

</div>

Hi, this works for JSON in SQlite and as been working for over a year, use any parts that are useful to you !  
I got the help a year ago, so just passing on what worked for me 😀

```auto

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

let device = msg.device; // The last part of the IP address.
let table = msg.table; // The database table name.

let new_date = new Date(); // Does NOT change with time zones across the world.
let iso = new Date().toISOString(); // RFC 3339 format ... for Grafana etc.
let timestamp = Math.round(new_date.getTime() / 1000); // getTime = milliseconds ... this removes last 3 digits to change to seconds ... for Grafana etc.

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

let data =
{
    payload: msg.data,
    payload_01: msg.payload_01,
    payload_02: msg.payload_02, // Anything that is not below listed below.
    temperature: msg.temperature, // The temperature value.
    humidity: msg.humidity, // The humidity value.
    sensor: msg.sensor, // Used for a sensor value that is not covered by others.
    battery: msg.battery, // The battery voltage.
    info: msg.info,
    cost: msg.cost,
    
    location: msg.location, // Where is it located in my home ?
    id: msg.id, // Description for database, that identifies this data from others that use the same shortened IP address.
    iso: iso, // Used by Grafana etc.
    timestamp: timestamp, // Used by Grafana etc.
};

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

data = JSON.stringify(data); // Converts a JavaScript value to a JSON string

const Data_sequence = `( device,device_data) values('${device}','${data}')` // This is the actual data.
const topic = `INSERT INTO ${table} ${Data_sequence}` // The insert string to send to database.

msg.payload = "";
msg.topic = topic;

return msg;

```

---

<div class="post-metadata">

### Author: ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)
#### Post date: [13 May 2025 19:35 UTC](https://discourse.nodered.org/t/sqlite-database-help/97023/4 "2025-05-13T19:35:14Z")

</div>

As E1cid mentioned the question mark is not sufficient anymore, in the past this was fine but is changed. You need to add the $ to your variable.  
For example.

```auto
msg2.topic = "INSERT INTO LichtSchak (timestamp, output2) VALUES ($timestamp, $output2)";
msg2.payload = [new Date(), msg.payload];

```

---

<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: [11 August 2025 19:36 UTC](https://discourse.nodered.org/t/sqlite-database-help/97023/5 "2025-08-11T19:36:07Z")

</div>

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