Closed idavollen closed 8 months ago
The error is precisely correct. You're closing the db immediately after scheduling the fs.readFile
.
If you want to close the db, you need to do it in your callback, after you run your db.exec()
.
It might help to go through a tutorial or two on how node schedules callbacks--as this shows, it can be decidedly confusing!
Thanks! It's my bad! it now works well after calling db.close() in the try-finally block. Before starting with better-sqlite3, I've investigated a lot and read diff between node-sqlite3 and the better-sqlite3, in brief async against sync. While trying better-sqlite3, my head is full of sync and have forgotten the async fs.readFile() :)
I spent my night starting with better-sqlite3, however, I've got stuck with the simple code snippet.
The foobar.sql just contains SQL for initially creating two tables:
My personal experiences of my initial playing with better-sqlite3 in three hours is that it lacks of good enough tutorials/documentations, why not make some code examples for those APIs helping new beginners to start with. When I googled that error message in the title, there are not too many hits. A good documentation will definitely help better-sqlite3 to speed up its popularity over node-sqlite3, IMHO