[英]Using MySQL db functions (?) with SQLite (Node.js)
我正在使用教程進行 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 中有很多專有功能在其他數據庫系統中不適用於標准 SQL。
這只是Mysql 和 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.