-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvparser.js
More file actions
172 lines (130 loc) · 3.69 KB
/
csvparser.js
File metadata and controls
172 lines (130 loc) · 3.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const parse = require('csv-parse')
const csvheads = require('csv-parser')
const fs = require('fs')
const mysql = require('mysql');
const express = require('express');
var csvHeaders = require('csv-headers');
const app =express();
//**CSV API WORKING */
const db =mysql.createConnection({
connectionLimit : 100,
host: 'localhost',
user :'root',
password:'',
database:'management'
});
db.connect((err)=>{
if(err){
throw err;
}
console.log('connection established..');
});
app.listen('3000',()=>{
console.log('server started on port 3000');
});
app.post('/column',(req,res)=>{
// let stream = fs.createReadStream('.\\files\\FL_insurance_sample.csv');
let myData = [];
fs.createReadStream('.\\files\\business-operations-survey-2019-international-engagement-csv.csv')
.pipe(
parse({
delimiter:',',
ltrim:true,
rtrim:true,
trim:true,
skip_lines_with_error:true
})
)
.on("data", function (dataRow) {
myData.push(dataRow);
})
.on("end", function () {
myData.shift();
const connection = mysql.createConnection({
host: 'localhost',
user :'root',
password:'',
database:'management'
});
// open the connection
connection.connect((err) => {
if(err)
{
console.log(err);
}else{
let sql = 'ALTER TABLE test DROP id';
connection.query(sql,(error, response) => {
console.log(error || response);
});
let query = 'INSERT INTO test VALUES ?';
connection.query(query, [myData], (error, response) => {
console.log(error || response);
});
}
});
});
});
app.post('/head', function(req,res){
/*
fs.ReadStream('.\\files\\Devices.csv','utf8',{pretty:true})
.pipe(csvheads({
delimiter: ',',
columns: true,
trim: true,
skip_empty_lines: true}))
.on('headers', (headers) => {
let dataarray = [];
dataarray.push(headers)
dataarray.trim()
let i = 0;
let dbhead = headers;
for (i ; i<dbhead.length ;i++){
db.query('ALTER TABLE test ADD '+dbhead[i]+' varchar(150)',function(err,result){
if(err)
{
console.log(err);
}
console.log(result);
});
}
})
*/
var options = {
file : '.\\files\\business-operations-survey-2019-international-engagement-csv.csv',
delimiter : ','
};
csvHeaders(options, function(err, headers) {
if(!err)
console.log(headers);
let i = 0;
let text = headers;
for (i ; i<text.length ;i++){
db.query('ALTER TABLE test ADD '+text[i]+' varchar(255) ',function(err,result){
if(err)
{
console.log(err);
}
console.log(result);
});
}
});
/*
app.post('/head', function(req,res){
const results = [];
fs.createReadStream('.\\files\\Devices.csv')
.pipe(parse({ltrim :true, rtrim :true,trim :true, delimiter:','}))
.on('headers', (headers) => {
results.push(headers)
let i = 0;
let text = headers;
for (i ; i<text.length ;i++){
db.query('ALTER TABLE test ADD '+text[i]+' text',function(err,result){
if(err)
{
console.log(err);
}
console.log(result);
});
}
}) */
})