In this tutorial, we will see how to connect a javascript code to a local database using nodejs. as a database, we will use sqlite3 and we will see how to make a simple javascript application so let's start.
Install sqlite3
first of all, we need to install the sqlite3 module using npm.
if you don't have nodejs I recommend you to install it by going to this link: Nodejs Download
to install sqlite3 just open your cmd and type this command:
>npm install sqlite3
now let's start coding
import the module
now that we have installed sqlite3 we need to import this module in our project. Inside your project file just add this line
const sqlite = require('sqlite3');
this line will allow you to use all the property and methods that provide the sqlite3 module, now we need to to create our first database, to do this let's declare a new variable and call it db_name and let's assign the database name to this variable:
now we want to create the database, and it's pretty simple, just use the Database method like this
now our database is created and it's stored inside our db variable we have used a throwing error because it's better to know if an error occurs while creating your project, it will allow you to debug faster.
Now that we have created our database we need to add some tables and some value to be able to use it.
note that you should learn SQL to use SQLite. so let me show you how to create a table in SQLite.
for this, we will use the run method
we have created a table called user and it will contain two columns, one for the id and another for the user name.
now let's add a value to this table. we will again use the run method and run an SQL query
now we need to read our database. we will use the get method and add a Select query like this
we should not forget to close our database using the close method.
and this is the final code:
and this is the output:
//declare the database name
const db_name = "Qflash_db.db";
now we want to create the database, and it's pretty simple, just use the Database method like this
let db = new sqlite.Database(db_name, err =>{
if(err){
throw err
}
console.log("database started on main.db");
});
now our database is created and it's stored inside our db variable we have used a throwing error because it's better to know if an error occurs while creating your project, it will allow you to debug faster.
Now that we have created our database we need to add some tables and some value to be able to use it.
note that you should learn SQL to use SQLite. so let me show you how to create a table in SQLite.
for this, we will use the run method
//Create Table inside our database
db.run(`CREATE TABLE user(id INTEGER PRIMARY KEY, user VARCHAR(255))`);
we have created a table called user and it will contain two columns, one for the id and another for the user name.
now let's add a value to this table. we will again use the run method and run an SQL query
//Insert a Value inside our DB
db.run(`INSERT INTO user(user) VALUES(?)`,["Doctor code"])
now we need to read our database. we will use the get method and add a Select query like this
//getting all dara in database
db.get(`SELECT * FROM user`, (err,data) => {
if(err)
throw err
console.log(data)
})
we should not forget to close our database using the close method.
and this is the final code:
const sqlite = require('sqlite3');
//declare the database name
const db_name = "Qflash_db.db";
//Data Base Connection code
let db = new sqlite.Database(db_name, err =>{
if(err){
throw err
}
console.log("database started on main.db");
});
console.log(db);
//Create Table inside our database
//db.run(`CREATE TABLE user(id INTEGER PRIMARY KEY, user VARCHAR(255))`);
//Insert a Value inside our DB
//db.run(`INSERT INTO user(user) VALUES(?)`,["Doctor code"])
//getting all dara in database
db.get(`SELECT * FROM user`, (err,data) => {
if(err)
throw err
console.log(data)
})
db.close();
and this is the output:
Database { open: false, filename: 'Qflash_db.db', mode: 65542 }
database started on main.db
{ id: 1, user: 'Doctor code' }
Connect JavaScript to Sqlite using Nodejs
Reviewed by Medics
on
April 30, 2020
Rating:
No comments: