N
N
nevantain2021-11-18 09:07:32
SQLite
nevantain, 2021-11-18 09:07:32

Why is data not being added to SQLite3 database in JS?

Here is my code:

const express = require('express');
const sqlite3 = require('sqlite3').verbose();

const db = new sqlite3.Database('users.db', (err) => {
  if(err){
    console.log(err.message);
  }

  console.log('Connected SQlite database.');
});

const app = express();

const index = '/home/chipher/Desktop/code/www/js/less6DB/index.html';
const auth = '/home/chipher/Desktop/code/www/js/less6DB/auth.html';
const about = '/home/chipher/Desktop/code/www/js/less6DB/about.html';

const urlencodedParser = express.urlencoded({extended: false});

app.get('/', function(req, res) {
  res.sendfile(index);
});

app.get('/auth', function(req, res) {
  res.sendfile(auth);
});

app.get('/about', function(req, res) {
  res.sendfile(about);
});


app.post('/auth', urlencodedParser, function(req, res) {
  if(!req.body){
    return res.sendStatus(400);
  }

  db.run(`INSERT INTO users(user_id, username, password),
VALUES ${Math.floor(Math.random() * 1000)}, ${req.body.username}, ${req.body.pass}`)
  console.log(req.body);
  res.send(`${req.body.username} - ${req.body.pass}`);
});

app.listen(3000, () => console.log('SERVER STARTED http://127.0.0.1:3000'));


And at startup everything is fine, but when a request is sent, an SQL error appears:
[Error: SQLITE_ERROR: near "188": syntax error
Emitted 'error' event on Statement instance at:
] {
  errno: 1,
  code: 'SQLITE_ERROR'
}

I don't understand what he wants.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-11-18
@nevantain

const stmt = db.prepare("INSERT INTO users (user_id, user_nick, password) VALUES(?,  ?, ?)");
stmt.run(user_id,  req.body.username, req.body.passwd);
stmt.finalize();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question