Implement SQLite Database In Nodejs

Posted By : Vivek Joshi | 26-Nov-2017

For Implementing SQLite database in nodejs application using following steps;

Step1:- Install the node sqlite3 module using the following command.

npm install sqlite3

sqlite3 is a nodejs add-ones to interact with the SQLite database in nodejs application so you have to build the sqlite3 nodejs executable file for

doing this install the node-gyp and build the node executable file.

sqlite3 also can be installed in the node WebKit application.

For installing sqlite3 into node webkit we have to create a build with the compatibility of node WebKit version and operating system compatibility.

following steps are used to create the build for node WebKit application.

Step 1.1:- install nw-gyp for compile the c/c++ program to nodejs build.

npm install node-gyp -g   (if not already install node-gyp)

step 1.2:- install sqlite3 for the build version with node WebKit version and system architecture

npm install sqlite3 --build-from-source --runtime=node-webkit --target_arch=ia32 --target=$(NODE_WEBKIT_VERSION)

for windows system install the node-gyp and related other c++ compiler and all the required dependancy 

npm install sqlite3 --build-from-source --runtime=node-webkit --target_arch=x64 --target="node webkit version" --save

Step 2:- After successfully install and build sqlite3 write the following code for connecting to the inMemory database of SQLite server.

var Sqlite3 = require('sqlite3').verbose();

SqliteDBObj = new Sqlite3.Database("path/to/dbfile",function(err){
	if(err){
console.log("error in connection sqlite database",err);
}else{
console.log("database connected successfully.."):
}	
		});

Step 3:- After successfully create the connection with SQLite database you can execute any query into the database.

var Sqlite3 = require('sqlite3').verbose();

SqliteDBObj = new Sqlite3.Database("path/to/dbfile",function(err){
if(err){
console.log("error in connection sqlite database",err);
}else{
console.log("database connected successfully.."):
}	
});


sqliteDBObje.all("sql Query",[parameters],funtion(err,rows){

if(err)
console.log("error in query execution",err);
else
console.log("query data ",rows);
});

In this all method the first argument is the query for getting database from the database 

and second argument is the parameters which are used in query you can define the "?" symbol in query to define parameters and give that parameter in an array it is optional if there is no any parameter in query 

and last is the callback function witch will execute when database get the result or error.

there are many other functions to execute the query in SQLite you can check for more at here. 

About Author

Author Image
Vivek Joshi

Vivek is Web App Developer in Java Technology and also working on NodeJS.

Request for Proposal

Name is required

Comment is required

Sending message..