将 JSON 插入 mysql json 列

问题描述 投票:0回答:1

我在尝试插入记录时收到此错误

代码:'ER_PARSE_ERROR',

错误号:1064,

sql消息:

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 '[{"fieldname":"files","originalname":"node-v20.11.1-x64.msi","encoding":"7bit","' at line 1

sql状态:'42000',

索引:0,

sql:

INSERT INTO product (firstName,lastName,files) VALUES ('test1', 'SLIM',  JSON_OBJECT([{"fieldname":"files","originalname":"node-v20.11.1-x64.msi","encoding":"7bit","mimetype":"application/octet-stream","destination":"uploads","filename":"node-v20.11.1-x64.msi","path":"uploads\\\\node-v20.11.1-x64.msi","size":26636288},{"fieldname":"files","originalname":"VSCodeUserSetup-x64-1.87.0.exe","encoding":"7bit","mimetype":"application/x-msdownload","destination":"uploads","filename":"VSCodeUserSetup-x64-1.87.0.exe","path":"uploads\\\\VSCodeUserSetup-x64-1.87.0.exe","size":98146456}]));

后端

app.post('/', upload.array('files'), (req,res) => {
    let firstName = req.body.firstName;
    let lastName = req.body.lastName;
    let filename = JSON.stringify(req.files);
    console.log("filename:" + filename);
    let postQuery = "INSERT INTO product (firstName,lastName,files) VALUES ('" + firstName + "', '" + lastName + "',  JSON_OBJECT(" + filename + "));";
    console.log(postQuery);
    connection.query(postQuery, (err,result,fields) => {
        if (err) { throw err };
        
        res.send('Response has been recorded...');
    })
})

mysql 表 mysql table

如何让它在文件列上插入 JSON

mysql reactjs json insert
1个回答
0
投票

可以直接插入json,无需使用

JSON_OBJECT
函数。 JSON_OBJECT 具有不同的语法,旨在动态创建 json。

© www.soinside.com 2019 - 2024. All rights reserved.