In my project , I have the following in index.js:
const sqlite3 = require('sqlite3').verbose();
// open the database
const db = new sqlite3.Database('./test.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the database.');
});
console.log('here');
db.close((err) => {
if (err) {
console.error(err.message);
}
console.log('Close the database connection.');
});
process.exit();
sqlite.js:
function create_table(db, json) {
// hard coding is cheating, mostly... make it dynamic!
db.serialize( () => {
db.run('create table if not exists '
+ 'data1 ('
+ 'id numeric primary key, '
+ 'userid numeric, '
+ 'title text, '
+ 'completed text)');
db.run('delete from data1'); //or drop the table first..
})
}
module.exports = {
create_table
};
When I run this at the command line
node index.js
I see just 'here' in the console and no new table is added to the test db. What am I doing wrong?
EDIT:
I realized that the process.exit() function is somehow preventing completion of the connection (I'm using this to test a block of code at beginning of a file with the effect of a 'break' in other languages)
