How to create a basic application with node.js and sqlite3
By: Ben Chatwin
Tags: Sqlite, Javascript Tips, JavaScript, Nodejs, Sql

A fast, simple and no-fuss guide to setup a node project with sqlite3. Enjoy 😎 Here is what we will get done:

Setup a node.js project Setup a basic sqlite3 database Create a table, insert some data and read that data

Simple! 🚀

What you will need

Have installed node.js (You can do that here ✅) Nice to have written a few lines of javascript before Nice to have used the terminal before

Creating a node.js project

Create a new project directory and create app.js. Below are the terminal commands but you can just as well create these in VScode or whatever editing software you are using 👍

mkdir node_demo

cd node_demo

touch app.js

Next, we are going to initialise our node project and follow the prompts:

npm init package name: (node_demo)

version: (1.0.0)

description: sqlite3 demo #You don't have to fill this in 😊

entry point: (app.js)

test command:

git repository:

keywords:

author: Ben Chatwin

license: (ISC)

Next, we are going to into sqlite3:

npm install sqlite3

Now we are ready to code! Here is it all with comments, I will bring down each line below.

const sqlite3 = require(‘sqlite3’);

let db = new sqlite3.Database(“./demo.db”, (err) => {

if (err) {

console.log(‘Error when creating the database’, err)

} else {

console.log(‘Database created!’)

createTable()

}

}) const createTable = () => {

console.log(“create database table contacts”);

db.run(“CREATE TABLE IF NOT EXISTS contacts(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)”);

} const insertData = () => {

console.log(“Insert data”)

db.run(‘INSERT INTO contacts (name) VALUES (?)’, [“contact 001”]);

} insertData() const readData = () => {

console.log(“Read data from contacts”);

db.all(“SELECT rowid AS id, name FROM contacts”, function(err, rows) {

rows.forEach(function(row) {

console.log(row.id + “: “ + row.name);

});

});

} readData() db.close();

The breakdown…

Importing packages that you want to work with, in this case, sqlite3 😊

const sqlite3 = require(‘sqlite3’);

Create a variable that will initiate the connection to our database

let db = new sqlite3.Database(“./demo.db”, (err) => {

if (err) {

console.log(‘Error when creating the database’, err)

} else {

console.log(‘Database created!’)

createTable() # This function is described below

}

})

Create a function that creates a table within our database

const createTable = () => {

console.log(“create database table contacts”);

db.run(“CREATE TABLE IF NOT EXISTS contacts(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)”);

}

Create a function that inserts data into our database

const insertData = () => {

console.log(“Insert data”)

db.run(‘INSERT INTO contacts (name) VALUES (?)’, [“contact 001”]);

}

Create a function that reads data from our database

const readData = () => {

console.log(“Read data from contacts”);

db.all(“SELECT rowid AS id, name FROM contacts”, function(err, rows) {

rows.forEach(function(row) {

console.log(row.id + “: “ + row.name);

});

});

}

Key takeaways

Programming doesn’t have to be difficult.

These are the building blocks for exciting business applications

sqlite3 gets you going fast!

Stay tuned for more node.js and sqlite3 in the future 😊