-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect.js
62 lines (57 loc) · 1.66 KB
/
connect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const sqlite3 = require("sqlite3").verbose();
// Connecting to or creating a new SQLite database file
const db = new sqlite3.Database(
"./blog.db",
sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE,
(err) => {
if (err) {
return console.error(err.message);
}
console.log("Connected to the SQlite database.");
}
);
// Serialize method ensures that database queries are executed sequentially
db.serialize(() => {
// Create the items table if it doesn't exist
db.run(
`CREATE TABLE IF NOT EXISTS users (
person_id VARCHAR(100) PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(321) NOT NULL,
status VARCHAR(100) NOT NULL
);`,
(err) => {
if (err) {
return console.error(err.message);
}
console.log("Created users table.");
db.run(
`
CREATE TABLE IF NOT EXISTS blogs (
blog_id VARCHAR(250) PRIMARY KEY,
blog_title VARCHAR(1000) NOT NULL,
blog TEXT NOT NULL,
person_id VARCHAR(100) NOT NULL,
url_name VARCHAR(1000) NOT NULL,
description VARCHAR(1000) NOT NULL,
FOREIGN KEY (person_id) REFERENCES users(person_id)
);`,
(err) => {
if (err) {
return console.error(err.message);
}
console.log("Created blogs table.");
// Clear the existing data in the products table
db.close((err) => {
if (err) {
return console.error(err.message);
}
console.log(
"Closed the database connection. Operation was successful."
);
});
}
);
}
);
});