【问题标题】:Updating a value in SQL (better-sqlite3) with Node.JS使用 Node.JS 更新 SQL (better-sqlite3) 中的值
【发布时间】:2019-03-02 23:10:31
【问题描述】:

我目前有一个包含每个人的人员数据库,他们拥有一个状态值。我正在尝试更改他们的状态值。

  const id = parseInt(req.params.id , 10);
  const { valid, messageObj } = validateId(id);
  if (!valid) {
    res.status(400).send(messageObj);
  }

  let { status, priority } = req.body;
  let people = db.prepare('select * from people').all();
  const person = people.find(person => person.id === id);    

  if(status !== 'none' & status == 'ready' || status == 'done'){
    let updates = db.query(
        'UPDATE people SET ? WHERE ?', 
         [{ status: status }, { id: id }]
    );
  }

我不断收到db.query is not a function 的错误,但我尝试的每个功能都会收到此错误。

对 SQL 很陌生,但只是想弄清楚这一点或任何对我有帮助的文档,因为 Better-sqlite3 在官方文档中没有任何 update 函数。

【问题讨论】:

    标签: sql node.js sqlite sql-update better-sqlite3


    【解决方案1】:

    我在better-sqlite3 API for the Database class 中找不到名为query() 的函数。我认为你需要prepare()Statement object,然后run()

    此外,列名不能作为绑定参数传递。您的查询应如下所示:

    UPDATE people SET status = ? WHERE name = ?
    

    你需要改变这个:

    let updates = 
        db.query('UPDATE people SET ? WHERE ?', [{ status: status }, { id: id }]);
    

    收件人:

    const stmt = db.prepare('UPDATE people SET status = ? WHERE id = ?'); 
    const updates = stmt.run(status, id);
    

    【讨论】:

    • 如果你愿意,可以压缩const info = db.prepare('update people set status = ? where id =?').run(status,id);
    猜你喜欢
    • 2020-09-03
    • 2019-10-11
    • 2022-11-10
    • 1970-01-01
    • 2018-04-12
    • 2020-09-01
    • 2019-08-05
    • 2012-09-04
    • 2020-04-22
    相关资源
    最近更新 更多