当列未知时,将数据存储在SQL数据库中的正确方法 [英] Correct way to store data in SQL database when columns are unknown

查看:143
本文介绍了当列未知时,将数据存储在SQL数据库中的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的情况是,我正在开发一个表单构建器,如应用程序,需要为所有用户定制。托管表单,并在数据库中收集响应。现在在数据库mysql中做正确的方法是什么。



一个例子假设两种形式,一种是文本字段,另一种是单选按钮和文本字段。还有一次,创建模型是有任何方式使用django表单,或者我将不得不采取其他方式。

解决方案

最近,mysql引入了 JSON字段


从MySQL 5.7.8开始,MySQL支持本机JSON数据类型,可以使
高效地访问JSON中的数据(JavaScript Object Notation)
文档。 JSON数据类型提供了在字符串列中存储
JSON格式字符串的优点:


即使你不有最新版本的mysql,仍然可以将JSON数据保存在varchar字段中,并且是许多第三方库为Django提供JSON支持。



需要第三方库的原因是因为Django不有一个内置的JSONField。最近添加了一个Postgresql,但mysql仍然落后。



不涉及mysql的替代方法是使用redis。 Django对 redis 的支持非常好,而且您知道redis散列与python dictionaries非常相似。 ORM支持需要与mysql json字段一样的第三方库。然而,将redis视为一个可以在会话中持久化并且查询速度非常快的python字典更为简单。最后但并非最不重要的是哈希只是冰山一角。


So the situation that I have is,I am developing a form builder like application which needs to be custom for all users. The form is hosted and response collected in database. Now what is the correct way to do the same in mysql like database.

For an example assume two forms, one with a text field and another with radio button and text field. Also once that model is created is there any way to use django forms, or will I have to go some other way.

解决方案

Recently mysql introduced JSON fields.

As of MySQL 5.7.8, MySQL supports a native JSON data type that enables efficient access to data in JSON (JavaScript Object Notation) documents. The JSON data type provides these advantages over storing JSON-format strings in a string column:

Even if you don't have the latest version of mysql it's still possible to save JSON data in a varchar field and is quite a popular solution supported by many third party libraries that provide JSON support for Django.

The reason that a third party library is needed is because Django doesn't have a built in JSONField. One has been added recently for Postgresql but mysql is still lagging behind.

Alternative that does not involve mysql is to use redis. Django has excellent support for redis and as you know redis hashes are very similar to python dictionaries. ORM support requires third party libraries as with mysql json fields. However it's simpler to think of redis as a python dictionary that can be persisted across sessions and queried very fast. Last but not least the hash is just the tip of the iceberge.

这篇关于当列未知时,将数据存储在SQL数据库中的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆