INSERT INTO 因 node-mysql 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21779528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:03:09  来源:igfitidea点击:

INSERT INTO fails with node-mysql

mysqlnode.jssql-insertnode-mysql

提问by rokirakan78

I am trying to insert some data with node.js. I have written the following code and installed MySQL support via npm, but I failed to INSERT INTOthe table.

我正在尝试使用 node.js 插入一些数据。我已经编写了以下代码并通过 npm 安装了 MySQL 支持,但我未能上INSERT INTO表。

Here is my code:

这是我的代码:

var mysql = require('mysql');

function BD() {
    var connection = mysql.createConnection({
        user: 'root',
        password: '',
        host: 'localhost',
        port: 3306,
        database: 'nodejs'
    });
    return connection;
}

app.post("/user/create", function(req, res) {
    var objBD = BD();

    var post = {
        username: req.body.username,
        password: req.body.password
    };

    objBD.query('INSERT INTO users VALUES ?', post, function(error) {
        if (error) {
            console.log(error.message);
        } else {
            console.log('success');    
        }
    });
});

HTML code:

HTML代码:

<form action="/user/create" method="POST" class="form-horizontal">
     <input type="text" id="username_input" name="username">
     <input type="text" id="password_input" name="password">
     <input type="submit" name="Submit" value="Insert" class="btn">
</form>

The error message is:

错误信息是:

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 .'username' = 'Test' , 'password' = 'test' at line 1

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行附近使用的正确语法 .'username' = 'Test' , 'password' = 'test'

What is my mistake?

我的错误是什么?

回答by TimWolla

Pay attention to the documentation of node-mysql:

请注意以下文档node-mysql

If you paid attention, you may have noticed that this escaping allows you to do neat things like this:

var post  = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
  // Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'

如果你注意了,你可能已经注意到这种转义可以让你做这样的事情:

var post  = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
  // Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'

Notice that they use SETinstead of VALUES. INSERT INTO ... SET x = yis a valid MySQL query, while INSERT INTO ... VALUES x = yis not.

请注意,它们使用SET代替VALUESINSERT INTO ... SET x = y是一个有效的 MySQL 查询,而INSERT INTO ... VALUES x = y不是。

回答by joeruzz

I know this is an old post but thought I'd share that INSERT INTO...VALUES is a valid command (no SET) and very useful for bulk updates:

我知道这是一篇旧帖子,但我想我会分享 INSERT INTO...VALUES 是一个有效的命令(没有 SET)并且对于批量更新非常有用:

db.query('INSERT INTO myTable (field1, field2, field3) VALUES ?', [values])

where [values] is an array of arrays, each array containing those three fields and inserted as a record in the table.

其中 [values] 是一个数组数组,每个数组包含这三个字段并作为记录插入表中。

回答by Branndon

For others searching, I had this same issue, but it turns out it was a more complicated, unrelated issue. I wasn't declaring 'use strict';at the very top of the document.

对于其他搜索者,我遇到了同样的问题,但事实证明这是一个更复杂、无关的问题。我没有'use strict';在文件的最顶部声明。

I know this in itself won't solve the database insert problem, but it pointed out parts of my entire script that were coded incorrectly, and once fixing those, it worked as expected.

我知道这本身并不能解决数据库插入问题,但它指出了我整个脚本中编码错误的部分,一旦修复了这些问题,它就会按预期工作。