How to use Sqlite in Node.js

Hi guys! In this tutorial I will explain how to use Sqlite in Node.js. I will install the module sqlite3 which allows us to use sqlite with Node.js.

So, open your command line and enter npm install sqlite3

Then, open a js file from the folder where you have installed sqlite3 and enter:

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('newdata'); // newdata is the name of my database

db.serialize(function() {
db.run("CREATE TABLE IF NOT EXISTS lorem (info TEXT)"); // I will create a new table if not exists

var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
stmt.run("Hello World!"); // I will insert in my database "Hello World"
stmt.finalize();

db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
console.log(row.id + ": " + row.info);
});
});

db.close();

Then, open your command line and enter: node name of your js file.

Video tutorial:









Leave a Comment