当它在数据库中找到用户时,我想更新我的一列。

这是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'

最佳答案

看起来您正在查询字符串currentViewer,而您想查询变量currentViewer所保存的内容。

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

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

插入语句也是如此:

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

关于javascript - 用nodejs更新mysql表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51121746/

10-11 02:20