如何在 node-postgres 的 COPY TO 查询中发送参数
How do I send in parameters in COPY TO-query in node-postgres
我正在 ubuntu 服务器上的 postgres 运行 中执行查询以将结果导出到 CSV。
查询是从一个 Node-API 调用的,它通过 node-postgres-driver(https://node-postgres.com/).
连接到 Postgres
查询如下所示:
var sqlText =
'Copy (SELECT company.name AS Company, SUM(sale.numbersold) AS NumberSold
FROM SALE LEFT JOIN company ON company.companyid = sale.companyid
WHERE sale.createdate BETWEEN AND GROUP BY company.name)
To \'/tmp/test.csv\' With CSV HEADER DELIMITER \',\'';
每当我尝试将此查询与参数一起使用时,我都会收到一个异常,即 postgres 需要 0 个参数并得到 2 个参数。如何将参数发送到这样的查询?
如果我像这样使用静态文本进行查询,它会起作用:
var sqlText = 'Copy (SELECT company.name AS Company, SUM(sale.numbersold) AS NumberSold
FROM SALE LEFT JOIN company ON company.companyid = sale.companyid
WHERE sale.createdate BETWEEN \'2018-01-01\' AND \'2018-01-01\'
GROUP BY company.name)
To \'/tmp/test.csv\' With CSV HEADER DELIMITER \',\'';
节点中的代码:
var params = ['2018-01-01', '2018-01-28']
let dbResult;
try {
dbResult = await GetStuff(sqlText, params);
} catch (err) {
}
COPY
是实用语句,因此不支持参数化执行。
要么注入参数 client-side,或者,如果您不愿意,注入 server-side 并在函数中实现动态 SQL。
示例显示在 Use function variable in dynamic COPY statement。
我正在 ubuntu 服务器上的 postgres 运行 中执行查询以将结果导出到 CSV。
查询是从一个 Node-API 调用的,它通过 node-postgres-driver(https://node-postgres.com/).
连接到 Postgres查询如下所示:
var sqlText =
'Copy (SELECT company.name AS Company, SUM(sale.numbersold) AS NumberSold
FROM SALE LEFT JOIN company ON company.companyid = sale.companyid
WHERE sale.createdate BETWEEN AND GROUP BY company.name)
To \'/tmp/test.csv\' With CSV HEADER DELIMITER \',\'';
每当我尝试将此查询与参数一起使用时,我都会收到一个异常,即 postgres 需要 0 个参数并得到 2 个参数。如何将参数发送到这样的查询?
如果我像这样使用静态文本进行查询,它会起作用:
var sqlText = 'Copy (SELECT company.name AS Company, SUM(sale.numbersold) AS NumberSold
FROM SALE LEFT JOIN company ON company.companyid = sale.companyid
WHERE sale.createdate BETWEEN \'2018-01-01\' AND \'2018-01-01\'
GROUP BY company.name)
To \'/tmp/test.csv\' With CSV HEADER DELIMITER \',\'';
节点中的代码:
var params = ['2018-01-01', '2018-01-28']
let dbResult;
try {
dbResult = await GetStuff(sqlText, params);
} catch (err) {
}
COPY
是实用语句,因此不支持参数化执行。
要么注入参数 client-side,或者,如果您不愿意,注入 server-side 并在函数中实现动态 SQL。
示例显示在 Use function variable in dynamic COPY statement。