Can I store a JavaScript Object in a mySQL database?

前端 未结 5 522
谎友^
谎友^ 2021-02-07 20:59

I am gathering data from a webpage visitor and putting it into a JavaScript object I create. But later I want to be able to reference the data they entered.

I have acce

5条回答
  •  醉梦人生
    2021-02-07 21:25

    Yes Actually It can be done.

    con.connect(function(err){
    var sql = "INSERT INTO books SET ?";  //books -- 
    if(err) console.dir("Error: "+err.message);
    var values = {
        "bname": "Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future",
        "author": "Elon Musk",
        "genre": "Technology",
        "bookcount": "5"
    };
    con.query(sql, [values], function(err, result){
       if (err) throw err;
       else console.dir("Successfully inserted the row in table.");
          console.log(result);
    });
    }); 
    

    But, along with this you have to satisfy these points,

    • value should be object i.e., value={ "name":"property" }
    • It should not be an array i.e., values = [ {}, {} ]
    • Imp. - It should not have primary id column.

    Hope it helps.

提交回复
热议问题