我了解到我无法再次使用方法db.all()
,因为无论我尝试向数据库询问什么,该调用都会引发错误:
错误:SQLITE_RANGE:列索引超出范围errno:25,代码:'SQLITE_RANGE'
我尝试执行"SELECT * FROM users"
,但仍然无法执行...
我想做的是获取“ widgets”行的实际值,并将客户端要求的新窗口小部件名称添加到此字符串中。因此,“ Widget1 Widget2”将变为“ Widget1 Widget2 Widget3”。我确定有正确的方法来执行此操作,但是过去2个小时我一直在尝试解决该问题,而我在Web开发方面的拙劣技能限制了我。任何可以解决这个问题的人将不胜感激!
代码:
var express = require("express");
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('.users.db');
router.get("/", function(req, res, next) {
var sqlParam = {
$username: req.query.user,
}
var sqlParam2 = {
$widgets: null,
$username: req.query.user,
}
db2 = db;
db.all('SELECT widgets FROM users WHERE username=$username', sqlParam, function(err, rows) {
if (err) {
console.log(err);
}
rows.forEach((row) => {
if (!row.widgets) {
sqlParam2.widgets = req.query.widget;
sqlParam2.username = req.query.user;
}
else {
sqlParam2.widgets = row.widgets + " " + req.query.widget;
sqlParam2.username = req.query.user;
}
})
console.log("UPDATE users SET widgets='" + sqlParam2.widgets + "' WHERE username='" + sqlParam2.username + "'");
db.all("UPDATE users SET widgets=$widgets WHERE username=$username", sqlParam2, function(err) {
if (err) {
console.log(err);
}
if (!err) {
res.send("ok");
}
else {
res.send("ko");
}
})
});
})
module.exports = router;