我想动态生成sql查询。我发现这个工具将Json对象转换为SQL表
http://querybuilder.js.org/demo.html
而且我有以下的JSON对象:
{
"condition": "AND",
"rules": [
{
"id": "name",
"field": "name",
"type": "string",
"input": "text",
"operator": "equal",
"value": "zura"
},
{
"condition": "OR",
"rules": [
{
"id": "category",
"field": "category",
"type": "integer",
"input": "select",
"operator": "equal",
"value": "1"
},
{
"id": "price",
"field": "price",
"type": "double",
"input": "number",
"operator": "equal",
"value": "123"
}
]
},
{
"id": "in_stock",
"field": "in_stock",
"type": "integer",
"input": "radio",
"operator": "equal",
"value": "1"
},
{
"condition": "AND",
"rules": [
{
"id": "category",
"field": "category",
"type": "integer",
"input": "select",
"operator": "equal",
"value": "2"
},
{
"id": "in_stock",
"field": "in_stock",
"type": "integer",
"input": "radio",
"operator": "equal",
"value": "0"
}
]
}
]
}
现在我想以正确保存这个JSON数据生成SQL表。 有什么办法可以生成表,如果是的话请给我链接或请帮我创建同一张表
我看不出键值如何转换为SQL数据类型,即'VARCHAR','TEXT'等(https://www.w3schools.com/sql/sql_datatypes_general.asp)。你可以使用CREATE TABLE语句(https://www.w3schools.com/sql/sql_create_table.asp),但根据你的JSON值,我不认为你会创建一个有效的SQL表。例如,从你的数据:'rules [“type”] = string','string'不是一个有效的SQL数据类型,而'VARCHAR(200)'是。如果您要动态生成基于对象的表格,我假设您至少需要正确的数据类型作为关键值。 – natureminded
数据类型不计量,首先我只需要Table(May Be tables)结构来保存我的JSON数据。我可以随时更改表格中的数据类型。 –
为了建立一个SQL表格的结构,你首先必须“CREATE TABLE”(见我最近的评论中的链接)。在创建表格时,必须定义其结构,这需要定义数据类型。 IE,SQL表必须知道期望的数据类型(数字,文本等)。据我所知,你不能创建一个SQL表,没有为你试图捕获的属性设置数据类型。这是创建表时的一个要求。很多人使用MySQL Workbench来编写他们的愿景,程序为他们吐出SQL表。 – natureminded