0

我需要使用我的 Nodejs 应用程序将数据插入到我的 oracle 数据库中。我正在使用 node-oracledb 框架来执行此操作。我可以用这个做 CURD 操作。当我尝试插入时间戳和日期字段(使用 to_date)时,它会抛出错误“ ORA-00932: inconsistent datatypes: expected TIMESTAMP got NUMBER\n

下面是我将用于插入 Oracle 数据库的输入参数。

  req.body.CREATE_TS = new Date('02-09-2014 00:30:00').getTime();
  req.body.EFF_DTE = new Date('07-12-2014 00:30:00').getTime();
  req.body.SENT_TS = new Date('07-20-2014 00:30:00').getTime();
  req.body.LAST_UPDT_TS = new Date('10-20-2014 00:30:00').getTime();
  req.body.EFF_DTE = new Date().toISOString().slice(0, 19).replace('T', ' ')

有什么方法可以将 JavaScript 日期转换为 Oracle 日期,将 JavaScript 日期时间转换为 Oracle 日期时间。TO_DATE()类似于和的东西TO_TIMESTAMP()

4

1 回答 1

1

你为什么在约会的时候打电话getTime?只需留下日期。

如果您显示更多代码会有所帮助。

鉴于此表:

create table t (
  c date
)
/

这是插入日期的示例:

const oracledb = require('oracledb');
const config = require('./dbConfig.js');

// date === 2017-01-01T00:00:00.000Z (UTC)
let date = new Date(Date.UTC(2017, 00, 01, 00, 00, 00));

async function insertDate() {
  const conn = await oracledb.getConnection(config);

  await conn.execute(
    'insert into t (c) values (:d)',
    {
      d: date
    },
    {
      autoCommit: true
    }
  );
}

insertDate();

最重要的部分是你得到正确的时区。如果您使用的是 DATE 或 TIMESTAMP 列,那么您应该格外小心。

请务必查看此文档: https ://github.com/oracle/node-oracledb/blob/master/doc/api.md#-9163-fetching-date-and-timestamps

最简单的解决方案是在运行 Node.js 之前设置 ORA_SDTZ 环境变量。

另外,请查看这些幻灯片(9-17 与日期相关): https ://www.dropbox.com/s/69jt5uu1fqus84c/Tips%20and%20Tricks%20for%20Getting%20Started%20with%20the%20Oracle%20Database% 20Driver%20for%20Node.pdf?dl=0

于 2017-11-10T23:11:30.007 回答