-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowdb.js
92 lines (78 loc) · 2.7 KB
/
showdb.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const sqlite = require("sqlite");
const sqlite3 = require("sqlite3");
const { open } = sqlite;
// Placeholder for the database file name
const dbFileName = "microblog.db";
async function showDatabaseContents() {
const db = await open({ filename: dbFileName, driver: sqlite3.Database });
console.log("Opening database file:", dbFileName);
// Check if the users table exists
const usersTableExists = await db.get(
`SELECT name FROM sqlite_master WHERE type='table' AND name='users';`
);
if (usersTableExists) {
console.log("Users table exists.");
const users = await db.all("SELECT * FROM users");
if (users.length > 0) {
console.log("Users:");
users.forEach((user) => {
console.log(user);
});
} else {
console.log("No users found.");
}
} else {
console.log("Users table does not exist.");
}
// Check if the posts table exists
const postsTableExists = await db.get(
`SELECT name FROM sqlite_master WHERE type='table' AND name='posts';`
);
if (postsTableExists) {
console.log("Posts table exists.");
const posts = await db.all("SELECT * FROM posts");
if (posts.length > 0) {
console.log("Posts:");
posts.forEach((post) => {
console.log(post);
});
} else {
console.log("No posts found.");
}
} else {
console.log("Posts table does not exist.");
}
await db.close();
}
showDatabaseContents().catch((err) => {
console.error("Error showing database contents:", err);
});
// const sqlite = require('sqlite');
// const sqlite3 = require('sqlite3');
// const { open } = sqlite;
// // Placeholder for the database file name
// const dbFileName = 'microblog.db';
// async function showDatabaseContents() {
// const db = await open({ filename: dbFileName, driver: sqlite3.Database });
// console.log('Opening database file:', dbFileName);
// // Check if the users table exists
// const usersTableExists = await db.get(`SELECT name FROM sqlite_master WHERE type='table' AND name='users';`);
// if (usersTableExists) {
// console.log('Users table exists.');
// const users = await db.all('SELECT username, hashedGoogleId FROM users');
// if (users.length > 0) {
// console.log('Users and their passwords:');
// users.forEach(user => {
// console.log(`Username: ${user.username}, Password: ${user.hashedGoogleId}`);
// });
// } else {
// console.log('No users found.');
// }
// } else {
// console.log('Users table does not exist.');
// }
// await db.close();
// }
// showDatabaseContents().catch(err => {
// console.error('Error showing database contents:', err);
// });