如何使用node.js将PSQL数据库导出到CSV文件

发布于 2025-02-08 19:43:42 字数 1167 浏览 0 评论 0原文

我想知道如何将psql命令行“转换”到我的“ queries.js”文件中的JavaScript函数中,以便当用户按下按钮时,它将数据库导出到我的应用程序的用户下载!

这是PSQL命令行:

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 :

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文