简体   繁体   中英

Android SQLite: Joins and cursor

I have the following tables in an sqlite database:

items
______
 _id (PK)
name
section_subsection_id (FK)

section_subsections
______
_id (PK)
section_id (FK)
subsection_id (FK)

subsections
______
_id (PK)
name

sections
______
_id (PK)
name

I would like to provide a certain keyword to subsections that would only grab only those that match this keyword under a limit, say x, and count all the items under this section AND subsection match.

I have used several queries, here is one:

String selectQuery = "Select subsections.name, subsections._id, temp.count as count 
FROM subsections LEFT JOIN 
sections_subsections ON subsections._id = sections_subsections.subsection_id 
JOIN items (SELECT count(items._id) as count from items) temp 
ON items.section_subsection_id = sections_subsections._id 
WHERE subsections.name LIKE 'keyword' AND sections_subsections.section_id = 1 ORDER BY
subsections.name ASC LIMIT 50 OFFSET 0 ";

When I try to iterate through the results, I get the list matching the keyword search but the count always displays the last count value from the result set. When I run the raw query in sqlite shell, I see the correct counts in the column with the respective rows, but iterating through the cursor in Android/Java seems to have a problem. Or possibly my query?

So for ListView in the app I would get same counts (that is all 20s), but in shell I see count with correct value. In fact, during cursor iteration, if I Log.d count to the screen it is also all 20s, yet the other column value name is different. What is wrong with my query? Or how do I correctly iterate through a table with multiple joins?

_id  name    count 
 ---------------   
1    item1   79
2    item2   30
3    item3   20

EDIT: I'm doing something like this in Java:

Cursor cursor = sqliteDatabase.rawQuery(selectQuery, null);
if (cursor != null) {
cursor.moveToFirst();
}

if (cursor.moveToFirst()) {
do {
SubSection subSection = new SubSection();                      
subSection.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID)));                     subSection.setSubSectionName(cursor.getString(cursor.getColumnIndex(KEY_TABLE_SUBSECTIONS_SUBSECTION_NAME)));
subSection.setRecords(cursor.getColumnIndex("count"));
subSections.add(subSection);
}
while 
(cursor.moveToNext());
}

try below query

Select subsections.name, subsections._id, (SELECT count(items._id) from items WHERE items.section_subsection_id = sections_subsections._id) as count 
FROM subsections LEFT JOIN 
sections_subsections ON subsections._id = sections_subsections.subsection_id 
WHERE subsections.name LIKE 'keyword' AND sections.name = 'Category' ORDER BY
subsections.name ASC LIMIT 50 OFFSET 0 ";

Thanks Syed Waqas, your answer is correct for joining. The problem was not my queries actually. It was my cursor call. I should have used: cursor.getInt(cursor.getColumnIndex("count")) instead of what I have in my original question. I don't know how I managed to not notice this big mistake. For everyone else, you can debug your cursor with the lovely DatabseUtils. :)

Log.d(LOG, "ROW: " + DatabaseUtils.dumpCursorToString(cursor)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM