javascriptnode.jsdatabaseexpressbetter-sqlite3

Create new table using better-sqlite3


I am trying to create a new table using node.js and better-sqlite3. When I execute the file, it returns the following error:

ReferenceError: createtable is not defined at Object.<anonymous> (...my file's path...)

How can I solve it? Here's my code:

const express = require('express');
var createError = require('http-errors');
const app = express();
const port = 3000;
const sqlitedb = require('better-sqlite3');

const createTable = "CREATE TABLE IF NOT EXISTS users('name' varchar, 'surname' varchar, 'date_of_birth' DATE DEFAULT, 'email' varchar, 'username' varchar PRIMARY KEY, 'password' varchar );"

const dbusers = './databaseUsers.sqlite3';
let db = new sqlitedb(dbusers , sqlitedb.OPEN_READWRITE, (err) => {
    if (err) {
        console.error(err.message);
    }
    verbose: console.log}
);

db.exec(createtable);

Thank you in advance ;)


Solution

  • You have a typo in the variable declaration. The variable you declared is createTable instead of createtable (note the capitalized T). So it should be:

    db.exec(createTable);