'Nodejs and SQLite how create multiple tables?

I have question about creating multiple tables in one DB. I try to run for 2 tables, but seems after the first db.run(CREATE TABLE , second db.run(CREATE TABLE does not create a second table in the DB, here is the code i used:

//database.js

var sqlite3 = require('sqlite3').verbose();
var md5 = require('md5');
const users = require("./users");
const usersStatistic = require("./users_statistic");
const DBSOURCE = "db.sqlite";

let db = new sqlite3.Database(DBSOURCE, (err) => {
    if (err) {
        // Cannot open database
        console.error(err.message)
        throw err
    }else{
        console.log('Connected to the SQlite database.');
        db.run(`CREATE TABLE user (
            id integer,
            first_name text, 
            last_name text,
            email text UNIQUE,
            gender text,
            ip_address text,
            CONSTRAINT email_unique UNIQUE (email)
            )`,(err) => {
            if (err) {
                // Table already created
            }else{
                // Table just created, creating some rows

                let props = '';

                for (let i = 0; i < Object.keys(users[0]).length; i++) {
                    if(i ===  Object.keys(users[0]).length - 1) {
                        props += `${Object.keys(users[0])[i]}`
                    }else {
                        props += `${Object.keys(users[0])[i]}, `
                    }
                }

                const insert = `INSERT INTO user (${props}) VALUES (?,?,?,?,?,?)`;

                for (let i = 0; i < users.length; i++) {
                    const user = users[i];

                    db.run(insert,
                        Object.values(user)
                        // user['id'],
                        // user['first_name'],
                        // user['last_name'],
                        // user['email'],
                        // user['gender'],
                        // user['ip_address']
                    )
                }
                // db.run(insert, [users[0]['first_name'], users[0]['last_name'], "[email protected]"])
                // db.run(insert, [users[1]['first_name'], users[2]['last_name'],"[email protected]"])
            }
        });
        //Start second table
        db.run(`CREATE TABLE user_statistic (
            user_id integer,
            date text, 
            page_views integer,
            clicks integer,
            )`,(err) => {
            if (err) {
                // Table already created
            }else{
                // Table just created, creating some rows
                let props = '';

                for (let i = 0; i < Object.keys(usersStatistic[0]).length; i++) {
                    if(i ===  Object.keys(usersStatistic[0]).length - 1) {
                        props += `${Object.keys(usersStatistic[0])[i]}`
                    }else {
                        props += `${Object.keys(usersStatistic[0])[i]}, `
                    }
                }
                const insert = `INSERT INTO user (${props}) VALUES (?,?,?,?)`;

                console.log(insert)
                for (let i = 0; i < usersStatistic.length; i++) {
                    const userStatistic = usersStatistic[i];
                    db.run(insert,
                        Object.values(userStatistic)
                    )
                }
            }
        });

    }
});

module.exports = db;



Solution 1:[1]

Hey there it has been a while but no one seems to reply.

I think you could try to use

db.serialize(()=>{
db.run([your 1st query])
db.run([your another query])
db.run([and another query])
}) 
db.close()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mr.Ko