# Using SQLite Data Base for temporary data

**URL:** https://discourse.nodered.org/t/using-sqlite-data-base-for-temporary-data/30463
**Category:** General
**Created:** [21 July 2020 11:31 UTC](https://discourse.nodered.org/t/using-sqlite-data-base-for-temporary-data/30463 "2020-07-21T11:31:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![e-Duardo](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@e-Duardo](https://discourse.nodered.org/u/e-Duardo)
#### Post date: [21 July 2020 11:31 UTC](https://discourse.nodered.org/t/using-sqlite-data-base-for-temporary-data/30463/1 "2020-07-21T11:31:49Z")

</div>

I am starting to use SQLite for storing some values from a continuous flow of input data.  
My purpose is that the data storage is temporary, and I don't want de DB file to grow forever.

Is there any SQL command (or any thing that I can do) to delete the oldest data so that the number of rows becomes steady? I mean some kind of FIFO...

I guess that I could use the DELETE sql command conditioned to a number given by the command COUNT, but I wonder I is there a more direct command...

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [21 July 2020 12:36 UTC](https://discourse.nodered.org/t/using-sqlite-data-base-for-temporary-data/30463/2 "2020-07-21T12:36:35Z")

</div>

SQL data isn't necessarily like that so you won't find trimming commands in a SQL db, you need to use a timeseries db like InfluxDB if you want to do that.

However, a delete can use a select statement as a filter (I'm fairly sure, my SQL is a bit rusty). So create a WHERE that selects the date/time values embedded in the data that you want to remove.

```auto
WHERE mydatefield <= 2020-01-01

```

for example (that may be invalid SQL though, as I say, I'm rather rusty.

Here are some alternatives:

> <https://dba.stackexchange.com/questions/11258/mysql-delete-all-rows-greater-than-n-entries-ordered-by-datetime>

---

<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: [21 July 2020 14:36 UTC](https://discourse.nodered.org/t/using-sqlite-data-base-for-temporary-data/30463/3 "2020-07-21T14:36:01Z")

</div>

I'm using this script in a function node to delete all the data from table **consoles** in my sqlite dbase that's older then 60 days.

```auto
var p_60d = 1000*60*60*24*60 ; //60 Days
var p_30d = 1000*60*60*24*30 ; //30 Days
var p_7d = 1000*60*60*24*7 ; //7 Days
var p_1d = 1000*60*60*24 ; // 1 Day
var p_1u = 1000*60*60; //1 Hour

var d = new Date();
var current = d.getTime();
var fromdate = current - p_60d;

msg.topic ="DELETE FROM consoles WHERE timestamp < " + fromdate + "" ;

return msg;

```

---

<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: [21 July 2020 14:59 UTC](https://discourse.nodered.org/t/using-sqlite-data-base-for-temporary-data/30463/4 "2020-07-21T14:59:36Z")

</div>

When you say temporary do you mean seconds, minutes, hours, days, weeks or what?

You might find that using persistent context storage might be much simpler that using sqlite. [https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

---

<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: [19 September 2020 14:59 UTC](https://discourse.nodered.org/t/using-sqlite-data-base-for-temporary-data/30463/5 "2020-09-19T14:59:47Z")

</div>

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