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:

23 thoughts on “NodeJS SQLite Tutorial”

  1. Thanks. This was just enough to break the ice for me. I’m in the middle of porting/translating a bunch of RUNbasic to Node.js. This helps a lot.

  2. I receive the following error during the installation:
    sqlite3@3.0.5 install c:node\node_modulessqlite3
    node-pre-gyp install –fallback-to-buld

    I work with:
    node v.0.11.14
    npm v.1.4.12

    Would appreciate some help.

          1. Thank you for your responses.
            Finally, I can install and successfully run sqlite3 v.2.2.3 with node.js v10.36. All this happens on an Intel Galileo running 32bit Windows (a tailored version of the os for this specific board). That means a lot of missing dependencies.
            Again, thank you for your help 🙂

    1. Changing dependancy to:

      “dependencies”: {
      “sqlite3”: “~3.1.4”

      Fixed this for me.

  3. I’m fairly new to a lot of this so thanks for this helpful post!

    One thing I don’t understand: why do you declare a variable “check”? you don’t employ it later and I haven’t found anything else online to suggest its use.


  4. Hi, it looks like you have some really fine software to offer.

    But could you please clean up the English grammar on this page. It is truly atrocious!

    Good luck with everything.

    Todd CS
    Houston, Texas, USA

  5. This is a great article..Do you have an idea as to how can i have this piece of code called from angular js or any html?

  6. Hi,“DELETE * from table_name where condition”);
    =>“DELETE from table_name where condition”);


  7. How I could check the number of records returned by a select query. in your example you are directly iterating.

  8. i received this error when i tried to run the command in my pc

    npm ERR! install Couldn’t read dependencies
    npm ERR! Windows_NT 10.0.10240
    npm ERR! argv “C:\Program Files\nodejs\node.exe” “C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js” “install”
    npm ERR! node v4.1.1
    npm ERR! npm v2.14.4
    npm ERR! path C:UsersBiniampackage.json
    npm ERR! errno -4058
    npm ERR! syscall open

    npm ERR! package.json ENOENT: no such file or directory, open ‘C:UsersBiniampackage.json’
    npm ERR! package.json This is most likely not a problem with npm itself.
    npm ERR! package.json npm can’t find a package.json file in your current directory.

    npm ERR! Please include the following file with any support request:
    npm ERR! C:UsersBiniamnpm-debug.log

  9. How to INSERT a javascript variable in SQLite?
    I tried:

    var smth1 = 1.2;
    var smth2 = 3.4;“CREATE TABLE if not exists mote1 (info TEXT)”);
    var stmt = db.prepare(“INSERT INTO mote1 (UV_index, UV_risk) VALUES (‘+ smth1 +’, ‘+ smth2 +’)”);;

    The result is not 1.2 and 3.4 as required but +smth1+ and +smth2+
    What is the correct syntax? (in nodejs)

Comments are closed.