You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
db.exec("CREATE TABLE IF NOT EXISTS student (id INT PRIMARY KEY, name CHAR(16), age INT, class CHAR(16), score FLOAT, info VARCHAR(255))");
52
+
db.exec("CREATE TABLE IF NOT EXISTS teacher (id INT PRIMARY KEY, name CHAR(16), age INT, info CHAR(255), INDEX (name))");
53
+
db.exec("CREATE TABLE IF NOT EXISTS book (id INT PRIMARY KEY, name CHAR(64), author CHAR(32), count INT, INDEX (name))");
54
+
console.log("Tables 'student','teacher' and 'book' created.");
55
+
56
+
// Clean (empty) all tables
57
+
db.exec("DELETE FROM student");
58
+
db.exec("DELETE FROM teacher");
59
+
db.exec("DELETE FROM book");
60
+
61
+
// Start a transaction
62
+
db.begin();
63
+
console.log("Begin transaction");
64
+
65
+
// Insert sample data into the student, teacher, and book tables
66
+
db.exec("INSERT INTO student (id,name,age,class,score) VALUES (1,'jack',10,'3-1',90),(2,'tom',11,'2-5',91),(3,'jack',11,'1-6',92),(4,'rose',10,'4-2',90),(5,'tim',10,'3-1',95)");
67
+
db.exec("INSERT INTO student (id,name,age,class,score,info) VALUES (6,'Tony',10,'3-1',95,'%s')", "He is a boy.\nHe likes playing football.\nWe all like him!");
68
+
db.exec("INSERT INTO student (id,name,age,class,score,info) VALUES (7,'Wendy',10,'3-1',95,'%s')", "She is a girl.\nShe likes cooking.\nWe all love her!");
69
+
db.exec("INSERT INTO teacher (id,name,age) VALUES (1,'Tomas',40),(2,'Steven',50),(3,'Bill',31),(4,'Lucy',29)");
70
+
db.exec("INSERT INTO book (id,name,author,count) VALUES (1,'Romeo and Juliet','Shakespeare',10),(2,'Pride and Prejudice','Austen',5),(3,'Great Expectations','Dickens',8),(4,'Sorrows of Young Werther','Von Goethe',4)");
71
+
console.log("Data inserted.");
72
+
73
+
// Query to select all students
74
+
let res =db.exec("SELECT * FROM student");
75
+
res.forEach((element,i) => {
76
+
console.log(i,"Select all students: ", element);
77
+
});
78
+
79
+
// Update a student's age and query the updated record
80
+
db.exec("UPDATE student set age=9 WHERE id = 2");
81
+
console.log("Updated age to 9 for student with id = 2");
82
+
83
+
res =db.exec("SELECT id,name,age,class,score from student WHERE id = 2");
84
+
res.forEach((element,i) => {
85
+
console.log(i,"Select student with id = 2: ", element);
86
+
});
87
+
88
+
// Delete a student and query the deleted record
89
+
db.exec("DELETE FROM student WHERE id = 3");
90
+
console.log("Deleted student with id = 3");
91
+
92
+
res =db.exec("SELECT * from student WHERE id = 3");
93
+
res.forEach((element,i) => {
94
+
console.log(i,"Select student with id = 3: ", element);
95
+
});
96
+
97
+
// Perform aggregation on the student table
98
+
res =db.exec("SELECT COUNT(*),MIN(score),MAX(score),SUM(score),AVG(score) FROM student");
// Update a student's age, query, and roll back the transaction
112
+
db.exec("UPDATE student set age=15 WHERE id = 2");
113
+
console.log("Updated age to 15 for student with id = 2");
114
+
115
+
res =db.exec("SELECT id,name,age,class,score from student WHERE id = 2");
116
+
res.forEach((element,i) => {
117
+
console.log(i,"Select student with id = 2: ", element);
118
+
});
119
+
120
+
db.rollback();
121
+
console.log("Transaction rolled back");
122
+
123
+
res =db.exec("SELECT id,name,age,class,score from student WHERE id = 2");
124
+
res.forEach((element,i) => {
125
+
console.log(i,"Select student with id = 2 after rollback: ", element);
126
+
});
127
+
128
+
// Execute multiple statements and query the results
129
+
res =db.exec("SELECT COUNT(*) as Count FROM student; SELECT id, name, age FROM student WHERE id=2;SELECT MIN(score) as min, MAX(score) as max, SUM(score) as sum, AVG(score) as avg FROM student");
console.log("Updated age to 15 for student with id = 2");
64
72
65
73
res=db.exec("SELECT id,name,age,class,score from student WHERE id = 2");
66
-
console.log("Select student with id = 2: ",res);
74
+
res.forEach((element,i)=>{
75
+
console.log(i,"Select student with id = 2: ",element);
76
+
});
67
77
68
78
db.rollback();
69
79
console.log("Transaction rolled back");
70
80
71
81
res=db.exec("SELECT id,name,age,class,score from student WHERE id = 2");
72
-
console.log("Select student with id = 2 after rollback: ",res);
82
+
res.forEach((element,i)=>{
83
+
console.log(i,"Select student with id = 2 after rollback: ",element);
84
+
});
73
85
74
86
// Execute multiple statements and query the results
75
-
res=db.exec("SELECT COUNT(*) as Count FROM student; SELECT id,name FROM student WHERE id=2");
76
-
console.log("Multi-statement select: ",res);
87
+
res=db.exec("SELECT COUNT(*) as Count FROM student; SELECT id, name, age FROM student WHERE id=2;SELECT MIN(score) as min, MAX(score) as max, SUM(score) as sum, AVG(score) as avg FROM student");
0 commit comments