首页 > 解决方案 > 使用 promise 将大数组插入 MariaDB

问题描述

我正在尝试将一个大对象插入我的数据库(mariaDB)。我不明白错误是来自我的数组格式还是来自我的 SQL 请求。

首先,我将此对象转换为我的查询的数组。

let values = jsonObj.reduce((o, f) => {
      let ini = []
ini.push(moment(f.date, "DD/MM/YYYY hh:mm").format("YYYY-MM-DD HH:mm:ss"))
      ini.push(parseInt(f.val))
      ini.push("x")
      o.push(ini)
      return o
    }, [])

在此之后,我使用我的 mariaDb 池进行查询:

let query = "INSERT INTO data_sycomore (date, valeur, codePS) VALUES ?"
let promise = new Promise((resolve, reject) => {
        pool.query(query, valuesQuery, (error, result) => {
          console.log(result)
          if (error) {
            reject(error)
          }
          else {
            resolve(result)
          }
        })
      })

我的表包含超过 50,000 个值。但是,我只插入对象的第一个价值游戏。我不明白问题出在哪里。预先感谢您的帮助。

大批 :

[[ '2019-04-01 03:00:00', -25, 'CODE1' ],
  [ '2019-04-01 03:10:00', -1112, 'CODE1' ],
  [ '2019-04-01 03:20:00', -1503, 'CODE1' ],
....]

这是我在控制台中遇到的错误:

(node:37125) UnhandledPromiseRejectionWarning: Error: (conn=822, no: 1241, SQLState: 21000) Operand should contain 1 column(s)
sql: INSERT INTO data_sycomore (date, valeur, codePS) VALUES (?) - parameters:[2019-04-01 00:00:00,-9017,CODE1,2019-04-01 00:10:00,-7381,CODE1,2019-04-01 00:20:00,-5798,CODE1,2019-04-01 00:30:00,-7594,CODE1,2019-04-01 00:40:00,-7011,CODE1,2019-04-01 00:50:...]
    at Object.module.exports.createError (x/node_modules/mariadb/lib/misc/errors.js:55:10)
    at PacketNodeEncoded.readError (x/node_modules/mariadb/lib/io/packet.js:523:19)
    at Query.readResponsePacket (x/node_modules/mariadb/lib/cmd/resultset.js:46:28)
    at PacketInputStream.receivePacketBasic (x/node_modules/mariadb/lib/io/packet-input-stream.js:104:9)
    at PacketInputStream.onData (x/node_modules/mariadb/lib/io/packet-input-stream.js:169:20)
    at Socket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:295:12)
    at readableAddChunk (_stream_readable.js:271:9)
    at Socket.Readable.push (_stream_readable.js:212:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
(node:37125) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:37125) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

标签: mysqlnode.jspromisemariadb

解决方案


推荐阅读