1

I have tried to use sqlite3 database for keeping data about workers that is entered and displayed from an html file

I have the code below

function insertValues() {    
        const sqlite3 = require('sqlite3').verbose();    
        alert('Ok');    
        let db = new sqlite3.Database('C:\Users\Root\Desktop\project'); 
        db.run('CREATE TABLE IF NOT EXISTS workers(id INTEGER PRIMARY KEY,names VARCHAR(30) NOT NULL,position VARCHAR(30) NOT NULL,date VARCHAR(10) NOT NULL,salary INTEGER)'); 
        const name = document.getElementById("addName");    
        const Nposition = document.getElementById("addPosition");   
        const startDate = document.getElementById("addStartDate");  
        const Nsalary = document.getElementById("addSalary");   
        db.run('INSERT INTO workers (id,names,position,date,salary) VALUES (NULL,?,?,?,?)', [name,Nposition,startDate,Nsalary], function(err){  
            if (err){            
                return console.log(err.message);        
            }       
            alert('Worker added');    
        }); 
        db.close()
}

Here the code doesn't seem to execute because I don't get the alert message when I click the button. But when the alert message is above everything, it shows in the webpage, making me think the error comes when importing, but I don't know what is wrong, below is the code when the alert message shows, and the button

function insertValues() {    
        alert('Ok');
        const sqlite3 = require('sqlite3').verbose();       
        let db = new  sqlite3.Database('C:\Users\Root\Desktop\project');    
        db.run('CREATE TABLE IF NOT EXISTS workers(id INTEGER PRIMARY KEY,names VARCHAR(30) NOT NULL,position VARCHAR(30) NOT NULL,date VARCHAR(10) NOT NULL,salary INTEGER)'); 
        const name = document.getElementById("addName");    
        const Nposition = document.getElementById("addPosition");   
        const startDate = document.getElementById("addStartDate");  
        const Nsalary = document.getElementById("addSalary");   
        db.run('INSERT INTO workers (id,names,position,date,salary) VALUES (NULL,?,?,?,?)', [name,Nposition,startDate,Nsalary], function(err){  
            if (err){            
                return console.log(err.message);        
            }       
            alert('Worker added');    
        }); 
        db.close()
}

And the button:

<button type="submit"  id="addRowButton" class="btn btn-primary" value="Send" onclick="insertValues()">Add</buttton>

It is inside button tags, didn't put them because it seemed to just show add in the draft...

When I run this same code in visual studio, with node.js, it works perfectly... Please help

K JACK
  • 11
  • 1

0 Answers0