Oz1*_*123 18 sqlite json sqlite-json1
SQLite现在有一个实验性的JSON1扩展来使用JSON字段.可供选择的函数看起来很有前途,但我不知道如何在查询的上下文中使用它们.
假设我创建了下表:
sqlite> create table user(name,phone);
sqlite> insert into user values('oz', json_array(['+491765','+498973']));
Run Code Online (Sandbox Code Playgroud)
该文档显示了如何json_each在查询中使用,但所有其他函数在上下文文档中缺少一些.
具有SQLite经验的人可以提供一些如何使用的示例:
json_extractjson_setOz1*_*123 34
所以,这是如何使用的第一个例子json_extract.首先,数据是以不同的方式插入的:
insert into user (name, phone) values("oz", json('{"cell":"+491765", "home":"+498973"}'));
Run Code Online (Sandbox Code Playgroud)
现在,我们可以像普通的sql一样选择所有用户的电话号码:
sqlite> select user.phone from user where user.name=='oz';
{"cell":"+491765","home":"+498973"}
sqlite>
Run Code Online (Sandbox Code Playgroud)
但是,如果我们不关心固定电话而我们只想要手机呢?
输入json_extract:
sqlite> select json_extract(user.phone, '$.cell') from user;
+491765
Run Code Online (Sandbox Code Playgroud)
这是如何使用json_extract.
使用json_set方法类似.鉴于我们想要更新手机:
sqlite> select json_set(json(user.phone), '$.cell', 123) from \
user;
{"cell":123,"home":"+498973"}
Run Code Online (Sandbox Code Playgroud)
您可以在其他SQL查询中组合这些函数调用.因此,您可以将SQLite与结构化数据一起使用,并以JSON的形式使用非结构化数据.
以下是仅更新用户手机的方法:
sqlite> update user
...> set phone =(select json_set(json(user.phone), '$.cell', 721) from user)
...> where name == 'oz';
sqlite> select * from user;
oz|{"cell":721,"home":"+498973"}
Run Code Online (Sandbox Code Playgroud)