nodejs + postgresql-“ s”处或附近的语法错误-Scanner_yyerror

时间:2019-07-10 09:39:14

标签: javascript node.js postgresql

我正在尝试将文本存储到表中,如下所示:

let insert_query = "insert into Messages(mid, mdate, sender, mtype, reply_to, is_forwarded, mtext, size, resolution, duration, emoji)"
insert_query += `values ('${message_id}', to_timestamp('${message_date}', 'DD.MM.YYYY HH24:MI:SS'), '${from_name}', '${media_type}', '${reply_to}', ${is_forwarded}, '${text}', '${media_size}', '${photo_resolution}', '${media_duration}', '${sticker_emoji}');`

这是一个相当长的查询,需要注意的重要部分是我在'${text}'字段中添加mtext的地方。

我收到此错误:

{ error: syntax error at or near "s"
    at Connection.parseE (/home/amirashabani/Amir/Code/telyzer/node_modules/pg/lib/connection.js:602:11)
    at Connection.parseMessage (/home/amirashabani/Amir/Code/telyzer/node_modules/pg/lib/connection.js:399:19)
    at Socket.<anonymous> (/home/amirashabani/Amir/Code/telyzer/node_modules/pg/lib/connection.js:121:22)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  name: 'error',
  length: 91,
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '251',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'scan.l',
  line: '1134',
  routine: 'scanner_yyerror' }

我打印出insert_query表示何时发生这些错误,下面是一些结果:

insert into Messages(mid, mdate, sender, mtype, reply_to, is_forwarded, mtext, size, resolution, duration, emoji)
values ('message8224', to_timestamp('21.06.2019 21:27:34', 'DD.MM.YYYY HH24:MI:SS'), 'AmirAShabani', 'text', '', false, 'he's a loser', '', '', '', '');

我可以看到问题出在单引号字符上。我试图用\'替换它,就像这样:

text = child['children'][0]['data'].trim().replace('\'', '\\\'')

但这也无济于事,而且我不确定我是否应该这样做。

我该如何解决?

0 个答案:

没有答案