如何使用node.js将PSQL数据库导出到CSV文件
我想知道如何将psql命令行“转换”到我的“ queries.js”文件中的JavaScript函数中,以便当用户按下按钮时,它将数据库导出到我的应用程序的用户下载!
这是PSQL命令行:
js函数i i创建了,但是有“一个”在tag_7z8eq73.csv附近或超过语法错误“:
async function exportDatabase(req, res){
return db.any('COPY (SELECT adc_i, adc_v FROM tag_7z8eq73) TO "tag_7z8eq73.csv" DELIMITER "|" CSV HEADER')
.then(rows => {
// console.log(rows);
res.json(rows)
})
.catch(error => {
console.log(error)
});
}
路由:
router.get('/export', db.exportDatabase);
按钮函数:
exportData(){
fetch('http://192.168.1.51:3000/api/v1/export', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
.then(res => res.text())
.then((result) => {
console.log('database exported')
})
.catch((error) => {
console.log(error)
});
},
谢谢那些会花时间回答的人!
I would like to know how to "translate" the psql command line into a javascript function in my "queries.js" file so that when a user press a button it exports the database to the user downloads of my application !
here is the psql command line :
The js function i created but there is "a syntaxe error near or over tag_7z8eq73.csv" :
async function exportDatabase(req, res){
return db.any('COPY (SELECT adc_i, adc_v FROM tag_7z8eq73) TO "tag_7z8eq73.csv" DELIMITER "|" CSV HEADER')
.then(rows => {
// console.log(rows);
res.json(rows)
})
.catch(error => {
console.log(error)
});
}
the route:
router.get('/export', db.exportDatabase);
the button function :
exportData(){
fetch('http://192.168.1.51:3000/api/v1/export', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
.then(res => res.text())
.then((result) => {
console.log('database exported')
})
.catch((error) => {
console.log(error)
});
},
Thank you to those who will take the time to answer !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论