-
Notifications
You must be signed in to change notification settings - Fork 0
/
initDb.js
51 lines (47 loc) · 1.36 KB
/
initDb.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
const sqlite3 = require("sqlite3").verbose();
// Connect to SQLite database, and if it doesn't exist, create it
const db = new sqlite3.Database(
"./collection.db",
sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE,
(err) => {
// Error handling for connection
if (err) {
return console.error(err.message);
} else {
// Success message for successful connection
console.log("Connected to the SQLite database.");
}
}
);
// Serialize runs to ensure sequential execution
db.serialize(() => {
// Run SQL command to create table if it doesn't exist
db.run(
`CREATE TABLE IF NOT EXISTS subs (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
subcount INTEGER NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)`,
function (err) {
if (err) {
throw err;
}
console.log("Created subs table");
}
);
db.run(`DELETE FROM subs`, function (err) {
// Error handling for deletion
if (err) {
throw err;
}
console.log("Deleted items in subs table");
});
// Close the database connection
db.close(function (err) {
if (err) {
throw err;
}
console.log("Closed the database connection.");
});
});