Reputation: 21
I am working in Node.JS with the Sqlite3 module. As part of my project I need to export the database as 'postcard.db'. I am able to create the table, but I am having difficulty finding ways to save my database with this .db extension. Can someone highlight which commands I could use to do this to my database. This is how I initialized my database:
const sqlite = require('sqlite3').verbose();
var db = new sqlite.Database('./postcards.db');
Clearly the second line is incorrect as no .db file is created in my project
Upvotes: 2
Views: 2545
Reputation: 12552
new sqlite.Database
will create the database if it does not exist. And when you close the handle it will be automatically saved. You don't have to do anything. Check this test file here:
describe('open and close non-existant database', function() {
before(function() {
helper.deleteFile('test/tmp/test_create.db');
});
var db;
it('should open the database', function(done) {
db = new sqlite3.Database('test/tmp/test_create.db', done);
});
it('should close the database', function(done) {
db.close(done);
});
it('should have created the file', function() {
assert.fileExists('test/tmp/test_create.db');
});
after(function() {
helper.deleteFile('test/tmp/test_create.db');
});
});
As you can see the test first deletes the file, then opens the database, closes the database then checks if the file exists or not then deletes the database.
So,
db = new sqlite3.Database('test/tmp/test_create.db', callback);
db.close(callback);
Should do what you are trying to achieve automatically.
Upvotes: 2