简体   繁体   中英

How to insert new MySQL JSON TYPES object from the browser in NODE.JS

I had this question about sql, json, and nodeJS. Is there a way I can insert something like this through node.js (Here's the link from oracle official website where I was referring to: enter link description here )

    INSERT INTO t1 VALUES('{"key1": "value1", "key2": "value2"}');

I'm trying to come up with a better data structure for my project. I've been using simple sql queries just like the ones from W3SCHOOL, but the more I keep working on my project, the more I realize that using JSON DATA TYPE would be more appropriate.

And here's how I was trying to insert an object from my server.js file, but without success!!!

app.get('/demo/add/', (req, res) => {
   const { content, author, ID} = req.query;
   const sql = `insert into demo values({'${content}':'content', '${author}':'author', '${ID}':3})`

   con.query(sql, (err, results) =>{
     if(err){
        return res.send(err)
     } else{
        return res.send('Successfully added a new content!!')
      }
    })
});

Previously, I had been using this method: http://localhost:4000/demo/add?key1=value&key2=value2... for simple query, but for now, I'm kind of stuck!!

Thank you in advance

I used json_extract() method to retrieve data from sql tables, and that's pretty much what my struggle was about.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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