0% found this document useful (0 votes)
0 views3 pages

nodejs

The JavaScript program creates a SQLite database named 'College' and a table 'Student' with fields for student number, name, and percentage. It inserts three student records and retrieves those with percentages between 35 and 75, displaying the results in a formatted table. The program handles database connection, table creation, data insertion, and querying efficiently.

Uploaded by

maahipavan007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

nodejs

The JavaScript program creates a SQLite database named 'College' and a table 'Student' with fields for student number, name, and percentage. It inserts three student records and retrieves those with percentages between 35 and 75, displaying the results in a formatted table. The program handles database connection, table creation, data insertion, and querying efficiently.

Uploaded by

maahipavan007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a JavaScript program to create a database named “College”.

Create a table named “Student”


with following fields (sno, sname, percentage). Insert 3 records of your choice. Display the names of
the students whose percentage is between 35 to 75 in a tabular format.

const sqlite3 = require('sqlite3').verbose();

const db = new sqlite3.Database('College.db', (err) => {

if (err) {

return console.error(err.message);

console.log("Connected to the College database.");

});

db.serialize(() => {

db.run(`CREATE TABLE IF NOT EXISTS Student (

sno INTEGER PRIMARY KEY,

sname TEXT NOT NULL,

percentage REAL NOT NULL

)`);

const stmt = db.prepare("INSERT INTO Student (sno, sname, percentage) VALUES (?, ?, ?)");

const students = [

[1, 'Alice', 40.5],

[2, 'Bob', 75.0],

[3, 'Charlie', 50.2]

];
students.forEach(student => {

stmt.run(student, (err) => {

if (err) console.error(err.message);

});

});

stmt.finalize();

console.log("\nStudents with percentage between 35 and 75:\n");

console.log("+----+---------+------------+");

console.log("| SNo | Name | Percentage |");

console.log("+----+---------+------------+");

db.each("SELECT * FROM Student WHERE percentage BETWEEN 35 AND 75", (err, row) => {

if (err) {

console.error(err.message);

} else {

console.log(`| ${row.sno.toString().padEnd(3)} | ${row.sname.padEnd(7)} | $


{row.percentage.toFixed(1).padStart(10)} |`);

}, () => {

console.log("+----+---------+------------+");

db.close();

});

});

Output:

You might also like