apollo24
apollo24

Reputation: 313

Using MySQL db functions (?) with SQLite (Node.js)

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?

Upvotes: 0

Views: 1029

Answers (1)

Artistan
Artistan

Reputation: 2037

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();

Upvotes: 1

Related Questions