Implement SQLite Database In Nodejs
Posted By : Vivek Joshi | 26-Nov-2017
For Implementing SQLite database in
Step1:- Install the node sqlite3 module using the following command.
npm install sqlite3
sqlite3 is a
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
following steps are used to create the build for node WebKit application.
Step 1.1:- install
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
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
there are many other functions to execute the query in SQLite you can check for more at here.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Vivek Joshi
Vivek is Web App Developer in Java Technology and also working on NodeJS.