NodeJS SQLite Tutorial

SQLite is a self-contained database engine that requires no server to run (for MySQL, Oracle we require a Database Server). SQLite is most popular in developing mobile apps and it is considered as most widely deployed database engine in the world.

Download code from Github.

NodeJS SQLite Tutorial

In this tutorial, I am going to explain how to use SQLite with NodeJS. Let’s create a project named “node-sqlite” and in that create a package file as “package.json”. Paste the following code in it.

"name": "node-sqlite",
"version": "1.0.1",
"dependencies": {
"sqlite3": "~2.2.3"

Install the project by typing “npm install” in the terminal or command prompt. It will install all dependencies and make your project ready to work.


SQLite Database Schema Setup

SQLite is a transactional database engine, which means you can run DDL and DML queries of SQL. DDL queries deal with the creation of database, tables, view etc. whereas DML queries are responsible for CRUD operation (insert, delete, update, etc.). Here is the code to create an SQLite database and performing SQL operations.

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('mydb.db');
var check;
db.serialize(function() {"CREATE TABLE if not exists user_info (info TEXT)");
var stmt = db.prepare("INSERT INTO user_info VALUES (?)");
for (var i = 0; i < 10; i++) {"Ipsum " + i);
<code lang="javascript">

db.each(“SELECT rowid AS id, info FROM user_info”, function(err, row) {
console.log( + “: ” +;


To run and analyse the code, type node filename.js (in my case app.js) and see whether it creates a “mydb.sql” file in your folder or not. This code will create a table in the database and add some numbers and displays them in the console.

SQLite output

SQLite Queries using NodeJS

To perform the SELECT query you should use the .all function and for other operations such as DELETE, UPDATE, INSERT you should use the .run function. Here have a look at simple code.

//Load modules
var sqlite3         =       require('sqlite3').verbose();
var db              =       new sqlite3.Database('./database_name.db');
<code lang="javascript">

//Perform SELECT Operation
db.all(“SELECT * from blah blah blah where this=”+that,function(err,rows){
//rows contain values while errors, well you can figure out.

<code lang="javascript">

//Perform INSERT operation.“INSERT into table_name(col1,col2,col3) VALUES (val1,val2,val3)”);

<code lang="javascript">

//Perform DELETE operation“DELETE * from table_name where condition”);

//Perform UPDATE operation"UPDATE table_name where condition");

Further reading:


Founder of Codeforgeek. Technologist. Published Author. Engineer. Content Creator. Teaching Everything I learn!

Articles: 288