MySQL数据库中存储了一些JSON数据,表模式如下:-
create TABLE ApplicationEntity (
-> name varchar(100),
-> jsonData longtext
-> ) ENGINE=InnoDB Default CHARSET=latin1;
表中 jsonData 的一个示例条目是这样的
[{"name":"some_name","description":"good_description","gla":"None","srcPort":"123","dstPort":"2345","disableTimeout":false"}]
我们如何访问更新的单个键和值对。
我尝试过这样的事情:-
mysql> Update ApplicationEntity set jsonData='%"gla":"google"%' where jsonData like '%"gla":"None"%';
Query OK, 5 rows affected (0.02 sec)
Rows matched: 5 Changed: 5 Warnings: 0
但是表格变成了这种形式
mysql> select * from ApplicationEntity;
+------+-------------------------------------------------------------------------------------------------------------------+
| name | jsonData |
+------+-------------------------------------------------------------------------------------------------------------------+
| a1 | %"gla":"google"% |
| a2 | %"gla":"google"% |
| a2 | %"gla":"google"% |
| a2 | %"gla":"google"% |
| a2 | %"alg":"google"% |
| a2 | [{"name":"CREATED_IN_CHROME","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}] |
| a2 | [{"name":"CREATED_IN_MOZILL","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}] |
+------+-------------------------------------------------------------------------------------------------------------------+
显然这没有用,我的问题是对于长文本数据类型,我们如何才能对各个字段进行更新。
所以表格应该变成这种形式:
+------+-----------------------------------------------------------------------------------------------------------------------+
| name | jsonData |
+------+-----------------------------------------------------------------------------------------------------------------------+
| a1 | [{"name":"CREATED_IN_IE","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}] |
| a2 | [{"name":"CREATED_IN_SAFARI","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}] |
| a2 | [{"name":"CREATED_IN_OMERTA","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}] |
| a2 | [{"name":"CREATED_IN_duckduckgo","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}] |
| a2 | [{"name":"CREATED_IN_ucbrowser","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}] |
| a2 | [{"name":"CREATED_IN_CHROME","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}] |
| a2 | [{"name":"CREATED_IN_MOZILL","description":"","gla":"google","srcPort":"","dstPort":"2345","disableTimeout":false}] |
+------+-----------------------------------------------------------------------------------------------------------------------+