Node.js 将Json文件数据转为SQL可执行的insert语句
生活随笔
收集整理的這篇文章主要介紹了
Node.js 将Json文件数据转为SQL可执行的insert语句
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
要轉(zhuǎn)換的json數(shù)據(jù)
[{"id": "1","name": "張三","height": "175.5","score": "88.5","hobby": "籃球"},{"id": "2","name": "李四","height": "178.0","score": "98.5","hobby": "足球"},{"id": "3","name": "王五","height": "179.0","score": "98.5","hobby": "乒乓球"},{"id": "4","name": "趙六","height": "172.5","score": "78.5","hobby": "籃球"} ]把data.json文件中的數(shù)據(jù)拼接成insert語句
/*把data.json文件中的數(shù)據(jù)拼接成insert語句 */ const path = require('path'); const fs = require('fs');fs.readFile(path.join(__dirname,'./','data.json'),'utf8',(err,content)=>{ // 讀取當前文件夾下的json文件,指定utf8編碼格式if(err) return;let list = JSON.parse(content); // 解析json數(shù)據(jù)let arr = [];list.forEach((stu)=>{let sql = `insert into student (name,height,score,hobby) values ('${stu.name}','${stu.height}','${stu.score}','${stu.hobby}');`;arr.push(sql); });fs.writeFile(path.join(__dirname,'data.sql'),arr.join(''),'utf8',(err)=>{console.log('init json data finished!');}); });運行結果:
insert into student (name,height,score,hobby) values ('張三','175.5','88.5','籃球'); insert into student (name,height,score,hobby) values ('李四','178.0','98.5','足球'); insert into student (name,height,score,hobby) values ('王五','179.0','98.5','乒乓球'); insert into student (name,height,score,hobby) values ('趙六','172.5','78.5','籃球');總結
以上是生活随笔為你收集整理的Node.js 将Json文件数据转为SQL可执行的insert语句的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nodejs Web网站-请求路径分发
- 下一篇: Node.js 连接数据库