【发布时间】:2020-02-25 20:24:16
【问题描述】:
我有一个名为 Label 的类:
class Label{
constructor(imageid, plotid, camera, date, x, y, w, h, id, hash){
this.imageid = imageid;
this.plotid = plotid;
this.camera = camera;
this.date = date;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.id = id;
this.hash = hash
}
}
我有一个名为 labels 的类对象的列表,我想插入,或者更确切地说是批量插入到我的 sqllite 数据库中,如下所示:
我试图插入这个类对象数组:
function addRowsToDb(){
console.log("Adding rows to the db")
db = new sqlite3.Database('./annotator.db');
db.serialize(function() {
var stmt = db.prepare("INSERT INTO Label(ImageID, PlotID, Camera, Date, x, y, w, h, LabelHash) VALUES (?,?,?,?,?,?,?,?,?)");
labels.forEach((label) => {
stmt.run(label.imageid, label.plotid, label.camera, label.date, label.x, label.y, label.w, label.h, label.hash);
})
stmt.finalize();
});
db.close();
}
这在控制台中给了我以下错误:
未捕获的错误:SQLITE_CONSTRAINT:NOT NULL 约束失败:Label.ImageID
未捕获的错误:SQLITE_CONSTRAINT:NOT NULL 约束失败:Label.LabelHash
有人可以向我解释如何正确执行此操作以及如何使用批量数据执行此操作,而不是使用标签数组的 forEach 吗?
【问题讨论】:
标签: javascript sql node.js sqlite