在 sqlite 中搜索来自 json 的值 - Android
Search value from json in sqlite - Android
我有 table 个名为 User 的列,其中包含名为 userDetails 的列。 userDetails 列具有如下 json 格式的数据。
{
"id": "77",
"uid": "247",
"email": "abc@xyz.com",
}
如何查询此 json 对象中存在的 'where uid = 247'。
如果列 userDetails
是有效的 JSON 字符串,您可以使用函数 json_extract()
:
select *
from user
where json_extract(userDetails, '$.uid') = '247'
如果您不能使用 SQLite 的 JSON1 扩展,您可以将 userDetails
视为普通字符串并使用运算符 LIKE
:
select *
from user
where ',' || replace(replace(replace(userDetails, '{', ''), '}', ''), ' ', '') || ',' like '%,"uid":"' || '247' ||'",%'
将 ?
占位符替换为您要搜索的 ID,例如 '247'
。
查看简化版 demo。
我有 table 个名为 User 的列,其中包含名为 userDetails 的列。 userDetails 列具有如下 json 格式的数据。
{
"id": "77",
"uid": "247",
"email": "abc@xyz.com",
}
如何查询此 json 对象中存在的 'where uid = 247'。
如果列 userDetails
是有效的 JSON 字符串,您可以使用函数 json_extract()
:
select *
from user
where json_extract(userDetails, '$.uid') = '247'
如果您不能使用 SQLite 的 JSON1 扩展,您可以将 userDetails
视为普通字符串并使用运算符 LIKE
:
select *
from user
where ',' || replace(replace(replace(userDetails, '{', ''), '}', ''), ' ', '') || ',' like '%,"uid":"' || '247' ||'",%'
将 ?
占位符替换为您要搜索的 ID,例如 '247'
。
查看简化版 demo。