This question already has answers here:
Database Structure for Tree Data Structure [closed]
(5 answers)
Closed 7 years ago.
I am using sqlite in android and need to make an select to get all parent with their child's, for example:
id || parent_id || child_id || name
1 1 1 jhon
2 1 2 helen
3 2 3 barack
4 1 4 manuel
5 3 5 gaga
result should be:
jhon
helen
manuel
barack
gaga
So, I need a reqursive sql, but googling a bit I found that CTE is not supported on sqlite, anyway I can use even an recursive java method to return a list of selected names order by parent asc
Notice that tree depth can be more than 2 levels!
I'm not sure how to interpret your table. Each node has an ID, right; and a (unique) parent_id (pointing to itself for the root node?). What's the child_id? Can't there be multiple children?
When dealing with recursive structures of arbitrary depth, if the tree doesn't change too often, and queries need to be fast, create a supporting table (say, "ancestral_closure") detailing the closure of all parent-child relationships:
ancestor_id, child_id
and make sure it's updated whenever the base table changes (recurse through the base table and add a row for each node that sits below another one). Join with the ancestral_closure table when you need to find all parents and/or children of a node. I don't think sqlite supports stored procedures executed on insert/delete/update triggers, so the update will have to be triggered by hand.
SQL is good at simple relations, not arbitrary graphs.
Related
I have an Android application using parse as a backend. One of my tables in my parse database contains groups of users, one row in this table contains 6 columns of users to form one group.
I would like to return each row where the current user is contained in one of the columns.
My question is what is the most optimum way of achieving this?As it stands I can think of two ways:1. query each row in the groups table and loop through each cloumn looking for the current users username.
2. if possible - use a parse query that says if currenUser equals member 1 or member 2 or member 3 or member 4 or member 5 or member 6. But I don't know if this is possible
Any pointers or help would be appreciated?
For your ParseQuery<ParseObject> query object a whereEqualTo method is available query.whereEqualTo(key, value); try using your column name in place or key and your desired value in in place of value.
Using this you will only get the rows which contains your desired values. It just works as the where clause in SQL qyery. so you will not need to iterate over all the DB entries.
In my application I have a tree structured table and I want to know that if I can fetch the tree structure data from sqlite database by simply applying a single query. Currently I am lopping it manually using a for loop and able to meet my requirement but I am not statisfied with that approach as its more complex, may be time consuming, etc.
My table table structure is as below,
parent_id | cat_id
null | 1
1 | 2
1 | 3
1 | 4
4 | 5
4 | 6
So, now above is my table structure and I want to fetch the tree structure like,
If I pass parent_id as 1 in my query then it should return 2,3,4,5,6 as 1 is parent of 1, 2, 3, 4 and 4 is parent of 5,6 and also 4 is child of 1
So, input and output expected by me,
input output
1 2,3,4,5,6
4 5, 6
So, it there a simple way to acheive the above tree structure by using sqlite query?
OR
There is no way to acheive this using sqlite query and I need to follow the current method or way that I am using it by manually looping all the category ids.
You can use group_concat method as it is available in sqlite.
For your question i have created one table called Trans and added parent_id and cat_id columns.
select parent_id, group_concat(cat_id) from trans group by parent_id;
I have table like this i have mentioned below.
and i got resultant like this :
Hope this will help you.
I didn't find any solution from sqlite itself, so created an Algorithm that creates Tree structure by iterating over each id & over its sub_ids and write into database.
I had gone through Closure Tables which gives the expected result that I was looking as a solution
Some more references of Closure Table -
http://technobytz.com/closure_table_store_hierarchical_data.html
http://karwin.blogspot.in/2010/03/rendering-trees-with-closure-tables.html
This question already has answers here:
How to get Top 5 records in SqLite?
(8 answers)
Closed 8 years ago.
My Android application has an activity to present data from SQLite database. The db table might contain huge number of rows. For performance reasons, I want to load 20 rows from db at a time, and when user scrolls down listview to the end, read next 20 rows.
So I want to use SQL statement like this:
select * from mytable where id > N and count = 20;
I just wonder if SQLite supports this kind of "count=20" feature to read at maximum 20 rows for the query. If it is supported, what is exact syntax?
Yes, it does. It is called LIMIT:
SELECT *
FROM mytable
WHERE id > N
LIMIT 20
You can also use optional OFFSET clause to start at certain row:
SELECT *
FROM mytable
WHERE id > N
LIMIT 20
OFFSET 100
You can use LIMIT and OFFSET to specify a set of rows to return.
However:
This is risky if other threads may be updating the database, particularly if your query will use ORDER BY.
Use tools like Traceview to really determine how long things take, and use that to determine the number of rows to fetch. 20 seems seriously annoying.
If your "table might contain huge number of rows", you should be focused on a search interface, not expecting people to browse linearly through some list that is long enough to warrant this sort of batching.
Please excuse me if its a repeated question. I tried searching here and at google but I couldn't exactly find what I wanted.
I have got two tables A & B.
Table A Fields : id, name, description, rating.
Table B Fields : id, aId (linked to table A), customerId, recommended.
Table A contains my data items for which I'm storing average cumulative ratings provided by users.
Table B stores another attribute for data of Table A. It stores the recommended bit (1 for recommended & 0 for non-recommended).
I want to list all the data from Table A but I want to sort them using recommended bit from Table B. So, if there are 10 records in Table A and 2 records in Table B, while listing all those 10 records, the two from Table B should come first and then the others from Table A. It doesn't matter whether the recommended bit value is a 0 or a 1. While listing the other 8 records from Table A, I want to list the records based on their rating in descending order.
Can someone please guide me in writing this sqlite query for Android app? Thanks in advance!
The left join adds the recommended field to the result set (with a value of NULL if there is no matching B record).
The expression recommended IS NULL or EXISTS(...) returns either 0 or 1:
SELECT DISTINCT A.*
FROM A LEFT JOIN B ON A.id = B.aId
ORDER BY B.recommended IS NULL,
A.rating DESC
Alternatively:
SELECT *
FROM A
ORDER BY NOT EXISTS (SELECT 1
FROM B
WHERE B.aId = A.id),
rating DESC
Im trying to do something like this (This is MySQL format)
SELECT q.question_id, q.question, q.answer
FROM relationship r, questions q
WHERE r.cat_id =1
AND r.question_id = q.question_id
LIMIT 0 , 30
So, I got questions stored in one table, then categories in another table. I have the relationship table set up so that a question can be in multiple categories. So say a question has an id of 5, and its in 3 different categories, the relationship table would look like this
relation_id, question_id, category_id
1 5 1
2 5 2
3 5 3
4 6 1
So, say I wanna get all the questions with the cat_id of 1, I should get 2 results. That's basically what I'm trying to do.
If you want all questions with cat_id of 1, then you want this:
select q.question, q.answer
from questions q
join relationship r on q.question_id = r.question_id
where r.cat_id = 1
I've switch to the ANSI join syntax rather than implied join conditions in the WHERE clause because the explicit version helps to avoid certain types of errors; in particular, ANSI joins help avoid accidental cross products and that's what your "MySQL format" query has because you neglected to include a join condition for category. That accidental cross product is almost certainly the source of your "returns each item 3 times" problem.
SELECT q.question, q.answer
FROM questions q
left join category c on q.catID = c.catID
left join relationship r on q.relID = r.relID
Some thing like this will de the trick