Creating Table in sqlite3 gives an error

I am currently trying to create a table in sqlite3 but every time I try to insert a new value it just gives an error:

undefined:0
[Error: SQLITE_ERROR: no such table: accounts
Emitted 'error' event on Statement instance at:
] {
  errno: 1,
  code: 'SQLITE_ERROR'
}

// CONSTANTS 
const server = require('http').createServer();
const io = require('socket.io')(server);
const moment = require('moment');
const now = moment().format('DD/MM/YYYY').toString();

// VARIABLES
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(':memory:');

db.run(`CREATE TABLE IF NOT EXISTS accounts (username TEXT,password TEXT,joined TEXT)`);
db.run(`INSERT INTO accounts VALUES ("TEST","TEST",`+now+`)`);

Does anyone know how to fix this?

You’d want to wait for the CREATE TABLE to run before starting on the INSERT. This package has a serialize wrapper thing to run them that way. See this doc https://github.com/mapbox/node-sqlite3/wiki/Control-Flow on the matter.

3 Likes