Popup

Wait! Don’t Go Yet! 👋

Become a Member Today and Unlock Access to All eBooks! 😍

Thousands of eBooks at your fingertips. Read, learn, and grow anytime, anywhere ✨

SQLite with Node-RED and Raspberry Pi

In this project, you’ll install an SQLite package for Node-RED and learn how to write simple SQL queries to interact with your SQLite database.

Recommended resources:

Basic Raspberry Pi setup

Before you continue reading this project, please make sure you have the Raspbian Operating System installed in your Raspberry Pi.

You can read my Getting Started with the Raspberry Pi Guide to install Raspbian and complete the basic setup.

Installing SQLite

Follow this next tutorial to learn how to Install SQLite database on a Raspberry Pi. Having SQLite installed on your Raspberry Pi.

Installing Node-RED SQLite

Run the list of commands below to install node-red-node-sqlite in your Node-RED user directory. This package gives you basic access to an SQLite database.

Top 6

Raspberry Pi eBooks

From Zero to Professional

Raspberry Pi Projects
pi@raspberry:~ $ sudo apt-get install npm
pi@raspberry:~ $ sudo npm install -g npm@2.x
pi@raspberry:~ $ hash -r
pi@raspberry:~ $ cd ~/.node-red
pi@raspberry:~/.node-red $ npm install node-red-node-sqlite

Restart your Node-RED software with the next commands for the changes to take effect:

pi@raspberrypi:~/.node-red $ cd
pi@raspberrypi:~ $ node-red-stop
pi@raspberrypi:~ $ node-red-start

Checking the installation

When your Node-RED software is back on, you can open it by entering the RPi IP address in a web browser, followed by :1880 as follows:

http://YOUR_RPi_IP_ADDRESS:1880

In my case is:

http://192.168.1.98:1880

A new node called sqlite should appear on the left under the storage tab:

Sqlite node red installed

Creating the flow

In this flow, you’re going to send 5 SQL queries (CREATE, INSERT, SELECT, DELETE, and DROP) to your SQLite database. Follow these next 10 steps to create your flow:

1) Drag 5 inject nodes, 1 sqlite node, and 1 debug node

Drag nodes

2) Press the Add new sqlitedb button

Configure sqlite f

3) Type /tmp/sqlite in the Database field

Sqlite default configs

4) Configure your CREATE inject node as follows

CREATE TABLE dhtreadings(id INTEGER PRIMARY KEY AUTOINCREMENT, temperature NUMERIC, humidity NUMERIC, currentdate DATE, currenttime TIME, device TEXT)
Configure your CREATE inject node as follows

5) Configure your INSERT inject node

INSERT INTO dhtreadings(temperature, humidity, currentdate, currenttime, device) values(22.4, 48, date('now'), time('now'), "manual")
Configure your INSERT inject node

6) Configure your SELECT inject node with

SELECT * FROM dhtreadings
 Configure your SELECT inject node

7) Configure your DELETE inject node as follows

DELETE from dhtreadings
Configure your DELETE inject node as follows

8) Configure your DROP inject node

DROP TABLE dhtreadings
Configure your DROP inject node

9) Connect all your nodes

Connect all your nodes

10) To save your application, you need to click the deploy button in the top right corner

Node-RED-deploy-button

Your application is saved and ready.

Testing the flow

Let’s test the simple flow. Open the debug window and press the first inject node to trigger the CREATE SQL query. Then, follow this procedure:

  1. CREATE
  2. INSERT
  3. SELECT
  4. INSERT
  5. SELECT
  6. DELETE
  7. DROP
Create select

As you can see, the database schema was created, data was inserted, you can select the data from the dhtreadings table, delete it, and drop the table.

Sqlite

This is a very basic example that can easily be extended for your own projects.

Wrapping up

This post is a quick guide to send simple SQL queries to an SQLite database with Node-RED. You can apply this concept to create a complex database that actually stores useful readings.

Do you have any questions? Leave a comment down below!

Share your love

🚀 Discover the world of electronics and innovation!

✨ Create, program, and experiment with all your creative ideas with ease.

Spotpear

Leave a Reply

Your email address will not be published. Required fields are marked *

Secure Payments
Securing online payments is a shared responsibility, and everyone can contribute.
Free Shipping
You get unlimited free shipping on eligible items with Ebokify, with no minimum spend.
24/7 Support
Sales gifts are helpful tools often used to show appreciation to clients for their purchase.
Gifts & Sales
Our customer care service is offered in the form of 1st or 2nd level support.