-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractData.js
More file actions
51 lines (46 loc) · 1.58 KB
/
extractData.js
File metadata and controls
51 lines (46 loc) · 1.58 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
const fs = require('fs');
const csv = require('csv-parser');
const mysql = require('mysql');
// Logger class for logging events
class Logger {
static log(message) {
console.log(`[INFO] ${new Date().toISOString()} - ${message}`);
}
static error(message) {
console.error(`[ERROR] ${new Date().toISOString()} - ${message}`);
}
}
// MySQL database connection configuration
const connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'root',
database: 'galacticmarketdatabase'
});
// Connect to MySQL database
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err);
return;
}
console.log('Connected to MySQL database');
});
// Read the CSV file and insert data into MySQL database
fs.createReadStream('GunCatalog.csv')
.pipe(csv())
.on('data', (row) => {
const { gun_name, gun_img, type, size, colors, price, description, ammo, availability } = row;
const sql = `INSERT INTO gunstable (gun_name, gun_img, type, size, colors, price, description, ammo, availability) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`;
connection.query(sql, [gun_name, gun_img, type, size, colors, price, description, ammo, availability], (err, result) => {
if (err) {
console.error('Error inserting data into MySQL:', err);
return;
}
console.log('Data inserted into MySQL:', result);
});
})
.on('end', () => {
console.log('CSV file successfully processed');
// Close MySQL connection
connection.end();
});