简体   繁体   English

无法使用 Node.JS 将 null 的值发送到 MySQL 数据库

[英]Can't send the value of null to MySQL database using Node.JS

I'm trying to send null to my MySQL database using Node.JS:我正在尝试使用 Node.JS 将null发送到我的 MySQL 数据库:

con.query("INSERT INTO Routes (routeTrigger) VALUES ( " + null + " )", {title: 'test'}, function(err, result) {
    if (err) throw err;
});

But when looking in the database, the updated value reads 'null' .但是在查看数据库时,更新后的值显示为'null' The database's NULL is supposed to read NULL .数据库的NULL应该读取NULL If I send capital NULL instead, that doesn't even work (in Node.JS).如果我改为发送大写NULL ,那甚至不起作用(在 Node.JS 中)。 If I send 'NULL' it sends the string "NULL", and not the database's NULL .如果我发送'NULL'它发送字符串“NULL”,而不是数据库的NULL

JavaScript's null is not the same as SQL's Null . JavaScript 的null与 SQL 的Null You want (in your case, using raw SQL strings) a string:您想要(在您的情况下,使用原始 SQL 字符串)一个字符串:

con.query("INSERT INTO Routes (routeTrigger) VALUES ( null )", {title: 'test'}, function(err, result) {
    if (err) throw err;
});

But note that that creates a row that's entirely null .但请注意,这会创建一个完全为null的行。 Which is possible, but it's unlikely what you want.这是可能的,但不太可能是您想要的。

What are you actually trying to do?你究竟想做什么?

If you want to assign the NULL value In the SQL tables, you can think differently.如果要在SQL表中分配NULL值,可以有不同的想法。

Instead of assigning NULL , you can tweak the field (column) so that the default value (when no value is assigned) is : NULL .您可以调整字段(列)而不是分配NULL ,以便默认值(未分配时)为: NULL

Now, when you insert your new row, just don't assign any value to that field .现在,当您插入新行时,不要为该字段分配任何值


Example :示例:

Let's say the table is structured like so :假设表格的结构如下:

MyTable我的表

id : auto increment; id : 自动递增; ... ...

name : text名称:文字

url : text;网址:文字; default = NULL默认值 = NULL

Then you could do:那么你可以这样做:

INSERT INTO Mytable ( name ) VALUES ( "Joe" );

As you omit the url information, it gets set to NULL .当您省略url信息时,它会被设置为NULL

To pass null value in mysql using nodejs, we need a different kind of implementation of a query.要使用 nodejs 在 mysql 中传递空值,我们需要一种不同类型的查询实现。

conn.query({
    sql: `INSERT INTO project values(?, ?, ?, ?);`,
    values: [projectId, name, startDate, endDate]}, (error, results) => {
        if (error) {
            console.log("Error executing sql: ", error.message);
            return res.send(error);
        }
    });
});

Official site says: "undefined / null are converted to NULL" and it works.官方网站说:“未定义/空值被转换为空值”并且它有效。

Use that above syntax to fire a query.使用上述语法来触发查询。

This was used for PostgresQL table migration from JSON using Node.js;这用于使用 Node.js 从 JSON 迁移 PostgresQL 表; may help.可能有帮助。 Check the values coming from your source, and if it doesn't exist for that column, use "NULL" with double quotes.检查来自您的源的值,如果该列不存在,请使用带双引号的“NULL”。 EG, if it's JSON: EG,如果是 JSON:

const makeVal = (val) => {
    if (val === undefined) {
        return "NULL"
    } else {
        return "'" + JSON.stringify(val).replace(/'/g, "''") + "'" 
    }
}

See this gist https://gist.github.com/foureyedraven/ca90a7dcbfd0918c153b0c91ec9317c5 for full implementation.有关完整实施,请参阅此要点https://gist.github.com/foureyedraven/ca90a7dcbfd0918c153b0c91ec9317c5

Using the Node mysql package I was able to insert a null value by doing the following:使用 Node mysql 包,我可以通过执行以下操作插入一个空值:

let myValue = null;

Note the column configuration:注意列配置:

'myValue' varchar(15) DEFAULT NULL

it worked它起作用了

if (my_var != null) {
    my_var = '"' + my_var + '"';
}                             
else {
    my_var = "NULL";
}
connection.query('insert into my_table(my_var) values(' + my_var + ')', (error, results) => {
    if (error) {
        throw error;
    }
    connection.end();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM