-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaddColumn.js
More file actions
executable file
·53 lines (45 loc) · 1.69 KB
/
Copy pathaddColumn.js
File metadata and controls
executable file
·53 lines (45 loc) · 1.69 KB
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
const sqlite3 = require('sqlite3').verbose();
// Open the database connection to 'poyoweb.db'
let db = new sqlite3.Database('poyoweb.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
console.error('Error opening database:', err.message);
} else {
console.log('Connected to the SQLite database "poyoweb.db".');
}
});
// Function to add a new column only if it doesn't exist
const addColumnIfNotExists = (tableName, columnName, columnType) => {
const checkColumnQuery = `PRAGMA table_info(${tableName});`;
db.all(checkColumnQuery, [], (err, rows) => {
if (err) {
console.error('Error checking table info:', err.message);
return;
}
// Check if the column already exists
const columnExists = rows.some(row => row.name === columnName);
if (!columnExists) {
const alterTableQuery = `ALTER TABLE ${tableName} ADD COLUMN ${columnName} ${columnType};`;
db.run(alterTableQuery, (err) => {
if (err) {
console.error(`Error adding column "${columnName}":`, err.message);
} else {
console.log(`Column "${columnName}" added successfully.`);
}
});
} else {
console.log(`Column "${columnName}" already exists in the "${tableName}" table.`);
}
});
};
// Add a column to the 'users' table if it doesn't exist
// addColumnIfNotExists('users', 'admin', 'INTEGER NOT NULL DEFAULT 0');
addColumnIfNotExists('websites', 'title', 'TEXT DEFAULT NULL');
addColumnIfNotExists('websites', 'description', 'TEXT DEFAULT NULL');
// Close the database connection
db.close((err) => {
if (err) {
console.error('Error closing database:', err.message);
} else {
console.log('Database connection closed.');
}
});