简体   繁体   English

选择,处理和插入/更新JSON数据类型

[英]Select, Handle and Insert/Update JSON data type

I am using a Node.JS server with a MySQL database and I just realised that MySQL supports JSON as a data type. 我正在使用带有MySQL数据库的Node.JS服务器,但我刚刚意识到MySQL支持将JSON作为数据类型。

Based on my previous statement how do I a) SELECT JSON , b) handle results in my node.js code and c) then UPDATE the DB entries again in JSON ? 根据我之前的声明,我如何a) SELECT JSON ,b)处理我的node.js代码中的结果,以及c)然后再次使用JSON UPDATE数据库条目?

Code example for parts a and b: a和b部分的代码示例:

sql.getConnection((err, con)=>{
            con.query("SELECT test FROM test", (error, row)=>{
            con.release();
            if(error) throw error;          
            console.log(row[0].test);
            });
});

this snippet of code returns: {"entryid": {"a": 1, "b": 2, "c": 3}} , 此代码段返回: {"entryid": {"a": 1, "b": 2, "c": 3}}

now if i try to do something like this: console.log(row[0].test./*any sub-key here*/); 现在,如果我尝试执行以下操作: console.log(row[0].test./*any sub-key here*/); it returns undefined . 它返回undefined

Well I managed to resolve my issue simply by ignoring MySQL's recommended syntax and implementing my own evil methods as you can see in this Gist . 好吧,我设法忽略了MySQL推荐的语法并实现了我自己的邪恶方法,从而解决了这个问题,正如您在Gist中所看到的那样。

let mysql = require('mysql');
let dbconn = {
    host: "localhost",       // make sure to replace with your own configuration
    user: "root",            // make sure to replace with your own configuration
    password: "password",    // make sure to replace with your own configuration
    connectionLimit: 100,    // make sure to replace with your own configuration
    database: "db"           // make sure to replace with your own configuration
};
let sql = mysql.createPool(dbconn);
let jsonObj;
 /*
    * let's assume that the stored JSON has the following structure:
    *
    * "master_key" : {
    *      sub_key1: "test1",
    *      sub_key2: "test2",
    *      sub_key3: {
    *          sub_key4: "test4"
    *      }
    *  
*/

sql.getConnection((err, conn) => {
    if(err) throw err;
    // We can SELECT it
    conn.query("SELECT json_Column FROM test_Table",(error, row) => {
        conn.release();
        if(error) throw error;
        jsonObj = JSON.parse(row[0].json_Column); //you can now handle the json keys as usual
        // jsonObj.master_key || jsonObj.master_key.sub_key1 || jsonObj.master_key.sub_key3.sub_key4 || however you want
    });

    // We can INSERT it
    jsonObj = {/*your JSON here*/};
    conn.query("INSERT INTO test_Table(json_Column) VALUES ?", [JSON.stringify(jsonObj)],(error, row) => {
        conn.release();
        if(error) throw error;
        console.log(row[0]);
    });

    // We can UPDATE it
    jsonObj = {/*your JSON here*/};
    conn.query("UPDATE test_Table SET json_Column = ?", [JSON.stringify(jsonObj)],(error, row) => {
        conn.release();
        if(error) throw error;
        console.log(row[0]);
    });
});

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

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