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))");
9
10
db.exec("CREATE TABLE IF NOT EXISTS teacher (id INT PRIMARY KEY, name CHAR(16), age INT, info CHAR(255), INDEX (name))");
10
11
db.exec("CREATE TABLE IF NOT EXISTS book (id INT PRIMARY KEY, name CHAR(64), author CHAR(32), count INT, INDEX (name))");
11
12
console.log("Tables 'student','teacher' and 'book' created.");
12
-
// clean table
13
+
14
+
// Clean (empty) all tables
13
15
db.exec("DELETE FROM student");
14
16
db.exec("DELETE FROM teacher");
15
17
db.exec("DELETE FROM book");
16
18
19
+
// Start a transaction
17
20
db.begin();
18
21
console.log("Begin transaction");
19
22
20
-
// Insert
23
+
// Insert sample data into the student, teacher, and book tables
21
24
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)");
22
25
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!");
23
26
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!");
24
27
db.exec("INSERT INTO teacher (id,name,age) VALUES (1,'Tomas',40),(2,'Steven',50),(3,'Bill',31),(4,'Lucy',29)");
25
28
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)");
26
29
console.log("Data inserted.");
27
30
28
-
// Select
31
+
// Query to select all students
29
32
letres=db.exec("SELECT * FROM student");
30
-
console.log("Select all student: ",res);
33
+
console.log("Select all students: ",res);
31
34
32
-
// Update
35
+
// Update a student's age and query the updated record
33
36
db.exec("UPDATE student set age=9 WHERE id = 2");
34
-
console.log("Update age = 9 for student with id = 2");
37
+
console.log("Updated age to 9 for student with id = 2");
35
38
36
39
res=db.exec("SELECT id,name,age,class,score from student WHERE id = 2");
37
40
console.log("Select student with id = 2: ",res);
38
41
42
+
// Delete a student and query the deleted record
39
43
db.exec("DELETE FROM student WHERE id = 3");
40
-
console.log("User with ID 3 deleted.");
44
+
console.log("Deleted student with id = 3");
41
45
42
46
res=db.exec("SELECT * from student WHERE id = 3");
43
47
console.log("Select student with id = 3: ",res);
44
48
45
-
// Aggregation function
49
+
// Perform aggregation on the student table
46
50
res=db.exec("SELECT COUNT(*),MIN(score),MAX(score),SUM(score),AVG(score) FROM student");
0 commit comments