【问题标题】:updating mysql table with nodejs用nodejs更新mysql表
【发布时间】:2018-07-01 08:44:07
【问题描述】:

当我发现用户已经在数据库中时,我想更新我的列。

这是viewers = ['freddiejefferson']

表:

kong_plays           10
freddiejefferson     10

我的代码:

function getPoints(){
              for (viewerCount = viewerAmount; viewerCount >= 0; viewerCount--) {
                currentViewer = viewers[viewerCount]
                connection.query("SELECT * FROM points WHERE twitchName = currentViewer", (err, result) => {

                  if(err) throw err; 
                  if (result === ''){
                    connection.query("INSERT INTO points (twitchName, points) VALUES (currentViewer, '10')", (err, result) => {
                      if(err) throw err;
                      console.log("Inserted into table")
                    });
                  }else{
                    connection.query("UPDATE points SET points= points+10 WHERE twitchName = currentViewer", (err, result) => {
                      if(err) throw err;
                      console.log("updated points")
                    });
                  }
                });
              }

        }

它输出的错误: Error: ER_BAD_FIELD_ERROR: Unknown column 'currentViewer' in 'where clause'

错误 1:

connection.query("INSERT INTO points (twitchName, points) VALUES (" + currentViewer " +, '10')", (err, result) => {

                                                          ^^^^^^^^^^^^^   (under current viewer)

SyntaxError: missing ) after argument list

错误 2:

throw err; // Rethrow non-MySQL errors
        ^

 Error: ER_BAD_FIELD_ERROR: Unknown column 'undefined' in 'where clause'

【问题讨论】:

    标签: javascript mysql node.js twitch


    【解决方案1】:

    看起来您正在查询字符串 currentViewer,而您却想查询变量 currentViewer 包含的任何内容。

    这可以通过将查询行更改为:

    connection.query("SELECT * FROM points WHERE twitchName = " + currentViewer, (err, result) => {

    同样适用于插入语句:

    connection.query("INSERT INTO points (twitchName, points) VALUES (" + currentViewer + ", '10')", (err, result) => {

    【讨论】:

    • 更新了我的答案。我弄乱了顺序。对不起。
    猜你喜欢
    • 1970-01-01
    • 2018-06-02
    • 2016-01-13
    • 1970-01-01
    • 2019-11-10
    • 2013-03-05
    • 2021-11-07
    • 2020-08-27
    • 2014-10-22
    相关资源
    最近更新 更多