Advertisement
Guest User

Untitled

a guest
May 20th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. const csv = require('csv-parser')
  2. const fs = require('fs')
  3.  
  4. function esc (str) {
  5. return str.replace(/'/g, "''")
  6. }
  7.  
  8. function main () {
  9. const rows = []
  10. fs.createReadStream('file.csv')
  11. .pipe(csv({separator: ';'}))
  12. .on('data', (row) => {
  13. rows.push({
  14. name: row['Col 1 Name'],
  15. code: row['Col 2 Name']
  16. })
  17. })
  18. .on('end', () => {
  19. console.log('INSERT INTO table (code, name) VALUES ')
  20. for (const row of rows) {
  21. console.log(`('${esc(row.code)}','${esc(row.name)}),`)
  22. }
  23. })
  24. }
  25.  
  26. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement