【问题标题】:Why does my sql multi-row insertion query contain a syntax error?为什么我的sql多行插入查询包含语法错误?
【发布时间】:2019-10-19 01:04:53
【问题描述】:

我正在使用 heroku 上的 nodejs 处理发布请求,并尝试将 json 插入 postgres 数据库。

const client = await pool.connect();
    var jsondata = req.body;
    var values = [];
    for(var i=0; i< jsondata.length; i++) {
                         values.push([jsondata[i].id,jsondata[i].title,jsondata[i].imageUrl,jsondata[i].orderPosition]);
                    }
const sqlQuery = 'INSERT INTO showme_gardens_tourMapTableTest (mapId, imageId, title, position) VALUES ?;';
console.log("$%$% " + values);                  
const  resultOfMapsInsert = await client.query(sqlQuery, [values]);

我收到语法错误

2019-10-15T13:08:35.238210+00:00 app[web.1]: $%$% 25,Wind in the willows,http://something.com,4
2019-10-15T13:08:35.244196+00:00 app[web.1]: { error: syntax error at or near "?"
2019-10-15T13:08:35.244199+00:00 app[web.1]: at Connection.parseE (/app/node_modules/pg/lib/connection.js:602:11)
2019-10-15T13:08:35.244202+00:00 app[web.1]: at Connection.parseMessage (/app/node_modules/pg/lib/connection.js:399:19)
2019-10-15T13:08:35.244204+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:121:22)
2019-10-15T13:08:35.244206+00:00 app[web.1]: at Socket.emit (events.js:198:13)
2019-10-15T13:08:35.244208+00:00 app[web.1]: at addChunk (_stream_readable.js:288:12)
2019-10-15T13:08:35.244210+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:269:11)
2019-10-15T13:08:35.244212+00:00 app[web.1]: at Socket.Readable.push (_stream_readable.js:224:10)
2019-10-15T13:08:35.244214+00:00 app[web.1]: at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
2019-10-15T13:08:35.244216+00:00 app[web.1]: name: 'error',
2019-10-15T13:08:35.244218+00:00 app[web.1]: length: 90,
2019-10-15T13:08:35.244220+00:00 app[web.1]: severity: 'ERROR',
2019-10-15T13:08:35.244222+00:00 app[web.1]: code: '42601',

【问题讨论】:

  • 你确定“?”就在这里?我不知道 pg 模块,但它肯定在 postgres 中无效。这个问题here 似乎暗示了同样的问题,快速查看节点的 pg 文档显示它使用常规的$1 语法。

标签: node.js postgresql


【解决方案1】:

如果您使用的是 pg 模块,那么查询可以构造如下:

const query = {
  text: 'INSERT INTO tableName (field1, field2) VALUES($1, $2)',
  values: [value1, value2],
}

let result = await client.query(query);

node-pg官方文档说:

参数化查询

如果您将参数传递给查询,您将希望避免将字符串连接参数直接连接到查询文本中。这会(并且经常会)导致 SQL 注入漏洞。 node-postgres 支持参数化查询,将您的查询文本和您的参数原封不动地传递到 PostgreSQL 服务器,在该服务器中,使用经过实战考验的参数替换代码安全地将参数替换到查询中。

对于多行,我会说使用pg-promise:

const pgp = require('pg-promise')(/* initialization options */);

// Database connection details;
const cn = {
    host: 'localhost', // 'localhost' is the default;
    port: 5432, // 5432 is the default;
    database: 'myDatabase',
    user: 'myUser',
    password: 'myPassword'
};
const db = pgp(cn);

const colset = new pgp.helpers.ColumnSet(['col1', 'col2'], {table: 'tablename'});

// data input values:
const values = [{col1: value1, col2:value2}, {col1: value3, col2: value4}];

// generating a multi-row insert query:
const query = pgp.helpers.insert(values, colset);
//=> INSERT INTO "tablename"("col1","col2") VALUES(value1,value2),(value3,value4)

await db.none(query); // executing the query

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 2017-04-23
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多