我可以在 mySQL 数据库中存储 JavaScript 对象吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10725870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I store a JavaScript Object in a mySQL database?
提问by hartste90
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.
我正在从网页访问者那里收集数据并将其放入我创建的 JavaScript 对象中。但后来我希望能够引用他们输入的数据。
I have access to a MySQL database, so is there a way for me to store this object there?
我可以访问 MySQL 数据库,那么有没有办法在那里存储这个对象?
I want to try and keep it in the object format instead of breaking it up into its separate parts.
我想尝试将它保留在对象格式中,而不是将其分解为单独的部分。
回答by Elliot Bonneville
Store a JSON.stringified
version of your object in the database, then JSON.parse
it when you want the object back again. It would look something like this:
JSON.stringified
将对象的一个版本存储在数据库中,然后JSON.parse
在您想要再次返回该对象时存储。它看起来像这样:
var myObj = {some: data, other: stuff};
var myObjString = JSON.stringify(myObj);
// store string in mySQL database here
// load string from database
var myJSONString = //mySQL database call
var myLoadedObj = JSON.parse(myJSONString);
回答by Quentin
No, you can't, at least not as it is.
不,你不能,至少不能像现在这样。
You might be able serialise it to a string. If it consists entirely of arrays, simple objects, and the other data types supported by the JSON format(which it probably will do if it is user supplied data (the main exception being if binary files are uploaded)), then you could use a JSON serializer to do so. Modern browsers provide the JSON objectfor this purpose and json2.jswill polyfill for older browsers. You could then store the string in the database.
您也许可以将其序列化为字符串。如果它完全由数组、简单对象和JSON 格式支持的其他数据类型组成(如果它是用户提供的数据,它可能会这样做(主要的例外是如果上传了二进制文件)),那么您可以使用JSON 序列化程序来这样做。现代浏览器为此提供了JSON 对象,而json2.js将为旧浏览器提供polyfill。然后,您可以将字符串存储在数据库中。
Not breaking it into separate parts does throw away the many of the advantages of using a relational database though (i.e. the relationships and the ability to perform useful searches).
不把它分成单独的部分确实会放弃使用关系数据库的许多优点(即关系和执行有用搜索的能力)。
回答by Adrian J. Moreno
Just store it as a JSON object in a field using the varchar or text data type.
只需使用 varchar 或 text 数据类型将其作为 JSON 对象存储在字段中。
回答by kevin628
Take a look at JSON.You can use something like jQueryto serialize form data and send it off to the server. Also you could just use plain old forms, and process input on the server side.
看看JSON。您可以使用jQuery 之类的东西来序列化表单数据并将其发送到服务器。您也可以只使用普通的旧表单,并在服务器端处理输入。
回答by kishore
Yes Actually It can be done.
是的,实际上可以做到。
con.connect(function(err){
var sql = "INSERT INTO books SET ?"; //books -- <tablename>
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.
value
应该是对象,即 value={ "name":"property" }- 它不应该是一个数组,即 values = [ {}, {} ]
- 进出口。- 它不应该有主 ID 列。
Hope it helps.
希望能帮助到你。