Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

javascript - Insert statement with where clause doesnt run on nodejs mysql

Code:

function setvalue(val) {

        idoutput=val;
        base62val=Base.encode(idoutput);
        var baseinsert = {ShortUrlCode:base62val};
        console.log(idoutput);
        console.log(base62val);
        con.query('INSERT INTO URLTABLE set ?  where ID = ?',[baseinsert,idoutput],function(err){
        if(err) console.log(err);
        })
        console.log("short inserted");
        con.end();
}

Error I am receiving:

{ [Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where ID = 38' at line 1]
  code: 'ER_PARSE_ERROR',
  errno: 1064,
  sqlState: '42000',
  index: 0 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
'INSERT INTO URLTABLE set ?  where ID = ?'

You forgot to mention column names that you want to "SET".

 'INSERT INTO URLTABLE SET column_name = ? (, column_name2 = ?,...) WHERE ID = ?'

is the correct syntax

EDIT:

Note that it is always better to "standardize" your SQL queries, in which case you better use the standard SQL syntax below for an INSERT (instead of using your specific MySQL syntax):

INSERT INTO URLTABLE (a, b, c) VALUES (?,?,?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...