I am trying to implement a recursive function in an Android application,
First I will try (recursive function) from the "cmd" windows:
I create a table:
CREATE TABLE tree(
id_tree integer PRIMARY KEY AUTOINCREMENT,
id_boss TEXT,
id_child TEXT,
answ TEXT);
Then insert some values:
INSERT INTO tree(id_boss,id_child,answ) VALUES('1','2','T');
INSERT INTO tree(id_boss,id_child,answ) VALUES('1','3','F');
INSERT INTO tree(id_boss,id_child,answ) VALUES('2','P1','T');
INSERT INTO tree(id_boss,id_child,answ) VALUES('2','4','F');
INSERT INTO tree(id_boss,id_child,answ) VALUES('3','P2','T');
INSERT INTO tree(id_boss,id_child,answ) VALUES('3','8','F');
Then I run a recursive query, which is now available for sqlite 3.8:
WITH RECURSIVE
under_alice(name,level) AS (
VALUES('1','0')
UNION ALL
SELECT tree.id_child, under_alice.level+1
FROM tree, under_alice
WHERE tree.id_boss=under_alice.name
ORDER BY 2 DESC
)
SELECT substr('..........',1,level*3) || name FROM under_alice;
It works great with the result:
1
...2
......4
......P1
...3
......8
......P2
Then I select id_child, where id_boss = '1' and answ = 'T' with this request;
WITH RECURSIVE
under_alice(name,level) AS (
VALUES('1','0')
UNION ALL
SELECT tree.id_child, under_alice.level+1 FROM tree, under_alice WHERE
tree.id_boss=under_alice.name and answ = 'T' and tree.id_boss='1'
ORDER BY 2 DESC
)
SELECT substr('..........',1,level*3) || name FROM under_alice;
It works great with this result:
1
...2
All this can be run on the cmd command line, but when I try to implement it in my Android application, I get some errors (for example, Can not Be Executed):
Here is my code to execute a request from my Android app:
try {
SQLiteDatabase db = dbHandler.getWritableDatabase();
String q = "WITH RECURSIVE
under_alice(name,level)
AS ( VALUES('1','0')
UNION ALL
SELECT tree.id_child, under_alice.level+1 FROM tree, under_alice
WHERE tree.id_boss=under_alice.name and answ = 'T' and tree.id_boss='1' ORDER BY 2 DESC)
SELECT max(name) as au FROM under_alice;";
Cursor eq = db.rawQuery(q, null);
eq.moveToFirst();
int au = eq.getColumnIndex("au");
String sau = eq.getString(au);
makeToast(""+sau);
} catch (Exception e) {
makeToast("failed query");
}
}
How do I do this job? Is there something wrong with my request?