在我的 main.js 檔案中,字串是“hello,0,,0,0”(如果您在陣列中讀取它,則在第二個位置為空)它需要能夠將 NULL 傳遞到資料庫中。
僅供參考:如果第二個位置是 0 并且不是空的,它將起作用
我試圖將字串“,”拆分為 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");
我收到了這個錯誤
ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect integer value: '' for column 'three' at row 1
我創建的表:CREATE TABLE TESTING(一個 VARCHAR(50),兩個 BOOLEAN,三個 INT(4),四個 INT(4) , 五個布林值,主鍵(名稱));
欄位都是 NULL
上表中沒有自動增量,我使用完全相同的方式創建了另一個表,但具有自動增量 id 但仍然拋出相同的錯誤
uj5u.com熱心網友回復:
既然你知道引數three是一個數字,你可以寫
var three = Val[2] || null;
我假設您稍后會執行類似的命令
db.query(sqlquery, [one, two, three, four, five]);
然后,如果Val[2] = "0",您得到three = "0",并且該字串值被資料庫接受為數字 0。
ButVal[2] = ""是一個假值,因此您得到three = null,并且希望資料庫接受 NULL 值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/411369.html
標籤:
上一篇:使用fetch時服務器端的空正文
