0

I'm using a tutorial to do JWT/bcryptjs auth and then INSERT into a SQlite table. Thing is the tutorial is for MySQL and I get errors like db.query is not a function and db.escape is not a function

The db :

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

const DBSOURCE = "./src/db/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.')
}
});

module.exports = db

Example query :

db.query(
`SELECT * FROM users WHERE LOWER(username) = LOWER(${db.escape(
  req.body.username
)});`,
(err, result) => {
  if (result.length) {
    return res.status(409).send({
      msg: 'This username is already in use!'
    });
  } else { .........

My best guess is that the functions are different?

How do I get this right?

1 Answer 1

1

There are a lot of proprietary functions in MySQL that will not work with standard SQL in other database systems.

That is just the beginning of the differences between Mysql and SQLite

Provide some query examples and we may be able to assist you with each one.

-- update after your addition of query code...

Here is an example of sqlite-nodejs

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

// open the database
let db = new sqlite3.Database('./db/chinook.db');

let sql = `SELECT * FROM users WHERE LOWER(username) = LOWER(?)`;

db.all(sql, [req.body.username], (err, rows) => {
    if (err) {
        throw err;
    }
    rows.forEach((row) => {
        console.log(row.name);
    });
});

// close the database connection
db.close();
Sign up to request clarification or add additional context in comments.

4 Comments

Thank u. I edited the post with an example query. How would u do it with SQLite?
did not run code with updated prepared statement, but it should be close to right. :)
db.run example with unescaped string ... stackoverflow.com/a/30080602/372215

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.