Android SQLite数据库递归查询

标签 android sqlite android-sqlite recursive-query

select @pv:=categoryID as 'categoryID', name, parentID from categories
join
(select @pv:=4) tmp
where parentID = @pv

以上查询适用于 MYSQL,但不适用于我的 android 移动 SQLite 数据库。 还有其他解决方案吗? enter image description here

最佳答案

SQLite 使用 common table expressions 而不是一些特定于供应商的语法在递归查询的 SQL 标准中定义:

WITH RECURSIVE subtree
AS (SELECT categoryID, name, parentID
    FROM categories
    WHERE categoryID = 4
    UNION ALL
    SELECT c.categoryID, c.name, c.parentID
    FROM categories AS c
    JOIN subtree ON c.parentID = subtree.categoryID)
SELECT *
FROM subtree

但是,CTE 仅在 SQLite 中可用 3.8.3或更高版本,仅适用于 Android 5.0 或更高版本。 在较早的Android版本中,您无法使用递归查询,必须单独获取每个级别的数据。

关于Android SQLite数据库递归查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22086892/

相关文章:

android.database.sqlite.SQLiteConstraintException : NOT NULL constraint failed: albums. 路径(代码 1299)

android - Android登录,但从mysql获取数据并添加sqlite

android - 在 SQLite 中存储浮点值会更改 Room DB 中的小数精度

java - 为什么构造函数不调用 onCreate() 函数

Android:没有用户初始化的来电号码?

android - 是否可以将视频复制到 android 剪贴板?

android - fragment 实例中带有 Otto 事件总线的 IllegalArgumentException

java - Android - Green Dao 多重交易

sqlite - SQLite/FoxPro更新无法使用=查找记录,但可以使用LIKE查找记录

java - 通过套接字或回调的连续服务器客户端通信