我正在使用 Node.js 中的 api 来插入数据。我在 Oracle 中有一个表,其结构如下
CREATE TABLE my_table (
id id_seq.nextval,
start_date DATE,
primary key(id)
);
现在我尝试像这样插入数据库
const connection = oracledb.getConnection(dbConfig)
let options = {
autoCommit: true,
bindDefs:[
{type: oracledb.NUMBER}, {type: oracledb.DATE}
]
}
let binds = [
[1,moment().format('YYYY-MM-DD HH:MM:SS')],
[2,moment().format('YYYY-MM-DD HH:MM:SS')]
]
const result = connection.executeMany('insert into my_table values(:1, :2, :3)', options, binds);
但是这个解决方案不起作用。有人可以确认我在哪里犯了错误吗?另外,如何添加动态自动增量 id 而不是硬编码值。
这很简单。 您有 2 列
CREATE TABLE my_table (
id id_seq.nextval,
start_date DATE,
primary key(id)
);
当您尝试插入数据时,Oracle 认为您已经拥有了所有列的自己的值,包括 id。 只是不要将任何值放入 id 列,如下所示:
insert into my_table (start_date) values (:1)
btw,在这里你可以找到另一种技巧:How to create id with AUTO_INCRMENT on Oracle?