【问题标题】:Using MySQL db functions (?) with SQLite (Node.js)将 MySQL 数据库函数 (?) 与 SQLite (Node.js) 一起使用
【发布时间】:2020-07-12 01:21:49
【问题描述】:

我正在使用 tutorial 进行 JWT/bcryptjs 身份验证,然后将 INSERT 用于 SQlite 桌子。 事情是这个教程是针对 MySQL 的,我得到了像 db.query is not a function 这样的错误 和db.escape is not a function

数据库:

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

查询示例:

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 { .........

我最好的猜测是功能不同?

我怎样才能做到这一点?

【问题讨论】:

    标签: mysql node.js sqlite


    【解决方案1】:

    MySQL 中有很多专有功能无法与其他数据库系统中的标准 SQL 一起使用。

    这只是differences between Mysql and SQLite的开始

    提供一些查询示例,我们可能会为您提供每一个查询示例。

    --添加查询代码后更新...

    这里是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();
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 2021-11-27
    • 1970-01-01
    相关资源
    最近更新 更多