简体   繁体   English

Express.js ER_TRUNCATED_WRONG_VALUE_FOR_FIELD:不正确的 integer 值:第 1 行的列 'id' 的''

[英]Express.js ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect integer value: '' for column 'id' at row 1

In my main.js file the string is "hello,0,,0,0" (empty at 2nd position if u read it in array) it need to be able to pass NULL into the database.在我的 main.js 文件中,字符串是“hello,0,,0,0”(如果您在数组中读取它,则在第二个 position 处为空)它需要能够将 NULL 传递到数据库中。
FYI: it will work if the 2nd position is a 0 and not empty仅供参考:如果第二个 position 为 0 且不为空,它将起作用
I am trying to split the string "," into var as shown below.我正在尝试将字符串“,”拆分为 var,如下所示。

var Val = req.body.string.split(","); 
var one = Val[0];
var two = Val[1];
var three = Val[2];
var four = Val[3];
var five = Val[4];
let sqlquery = "INSERT INTO TESTING (one, two, three, four, five) VALUES (?,?,?,?,?)";

let newrecord = [one, two, three, four, five];  
db.query(sqlquery, newrecord, (err, result) => {
if (err) {
return console.error(err.message);
} else
res.send("Added in database");

and i got this error我得到了这个错误
ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect integer value: '' for column 'three' at row 1 ER_TRUNCATED_WRONG_VALUE_FOR_FIELD:不正确的 integer 值:第 1 行的列“三”的“”

The table i created: CREATE TABLE TESTING (one VARCHAR(50), two BOOLEAN, three INT(4), four INT(4), five BOOLEAN,PRIMARY KEY(name));我创建的表:CREATE TABLE TESTING(一个VARCHAR(50),两个BOOLEAN,三个INT(4),四个INT(4),五个BOOLEAN,PRIMARY KEY(名称));
fields are all NULL字段都是 NULL
No auto increment in the table above, there is another table i created using the exact same way but with auto increment id but still throws the same error上表中没有自动增量,我使用完全相同的方式创建了另一个表,但具有自动增量 ID,但仍然抛出相同的错误

Since you know that parameter three is a number, you could write既然你知道参数three是一个数字,你可以写

var three = Val[2] || null;

I assume you later execute a command like我假设您稍后会执行类似的命令

db.query(sqlquery, [one, two, three, four, five]);

Then, if Val[2] = "0" , you get three = "0" , and this string value is accepted by the database for a numeric 0.然后,如果Val[2] = "0" ,你会得到three = "0" ,并且这个字符串值被数据库接受为数字 0。

But if Val[2] = "" , you get three = null , and this is hopefully accepted by the database for a NULL value.但是如果Val[2] = "" ,你会得到three = null ,并且希望数据库接受 NULL 值。

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

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