Reputation: 278
I have the following class called 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
}
}
I have a list of these class objects called labels
that I want to insert, or rather bulk insert into my sqllite database, which looks like the following:
I tried to insert this array of class objects with:
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();
}
Which gave me the following errors in the console:
Uncaught Error: SQLITE_CONSTRAINT: NOT NULL constraint failed: Label.ImageID
Uncaught Error: SQLITE_CONSTRAINT: NOT NULL constraint failed: Label.LabelHash
Could someone explain to me how to do this properly and how to do this with bulk data, so not the forEach with the labels array?
Upvotes: 0
Views: 457
Reputation: 26871
I think the error is pretty straight forward - you try to insert NULL
values into non-nullable db columns (Label.ImageID
and Label.LabelHash
).
How to solve this depends on what makes the most sense for your app:
NOT NULL
constraint on those columnsImageID
and LabelHash
setImageID
and LabelHash
colsUpvotes: 1