用nodejs更新mysql表

t1qtbnec  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(465)

当我的一列发现用户已经在数据库中时,我想更新它。
这是 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'
o2rvlv0m

o2rvlv0m1#

看起来你在查询字符串 currentViewer 相反,您希望查询变量 currentViewer 持有。
这可以通过将查询行更改为: connection.query("SELECT * FROM points WHERE twitchName = " + currentViewer, (err, result) => { 这同样适用于insert语句: connection.query("INSERT INTO points (twitchName, points) VALUES (" + currentViewer + ", '10')", (err, result) => {

相关问题