Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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]);
- });
- });
Add Comment
Please, Sign In to add comment