-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_data.js
More file actions
34 lines (28 loc) · 926 Bytes
/
import_data.js
File metadata and controls
34 lines (28 loc) · 926 Bytes
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
const fs = require('fs');
const sqlite3 = require('sqlite3');
const { open } = require('sqlite');
async function importData() {
// Open SQLite database connection
const db = await open({
filename: './questions.sqlite',
driver: sqlite3.Database
});
// Read the JSON file
const data = JSON.parse(fs.readFileSync('db.json', 'utf8'));
// Insert data into the questions table
for (const question of data.questions) {
await db.run(
'INSERT INTO questions (id, title, body, category, rank) VALUES (?, ?, ?, ?, ?)',
[question.id, question.title, question.body, question.category, question.rank]
);
}
// Close the database connection
await db.close();
}
importData()
.then(() => {
console.log('Data imported successfully');
})
.catch((err) => {
console.error('Error importing data:', err.message);
});