では、使用方法の最初の例を次に示しますjson_extract
。まず、データは少し異なる方法で挿入されます。
insert into user (name, phone) values("oz", json('{"cell":"+491765", "home":"+498973"}'));
これで、通常の SQL のようにすべてのユーザーの電話番号を選択できます。
sqlite> select user.phone from user where user.name=='oz';
{"cell":"+491765","home":"+498973"}
sqlite>
しかし、固定電話を気にせず、携帯電話だけが欲しい場合はどうなるでしょうか。
入力json_extract
:
sqlite> select json_extract(user.phone, '$.cell') from user;
+491765
そして、これが使い方json_extract
です。
使い方json_set
は似ています。携帯電話を更新したい場合:
sqlite> select json_set(user.phone, '$.cell', 123) from \
user;
{"cell":123,"home":"+498973"}
これらの関数呼び出しを他の SQL クエリで組み合わせることができます。したがって、SQLite を構造化データと JSON 形式の非構造化データで使用できます。
ユーザーの携帯電話のみを更新する方法は次のとおりです。
sqlite> update user
...> set phone =(select json_set(user.phone, '$.cell', 721) from user)
...> where name == 'oz';
sqlite> select * from user;
oz|{"cell":721,"home":"+498973"}