开发者

How to join multiple tables with managedQuery?

I'm trying to make a query where I get data from two tables but it won't show me any result. I know there is a 开发者_运维知识库result because in SQLite3 it displays at least one, e.g.

sqlite> select eventthemename, eventtypename from event_type, event_theme
   ...> where event_type.eventthemes = event_theme._id     
   ...> and event_type._id = '2';
Tribal | Dance

I'm using a content provider. Does anyone have an idea on how to make this?


Your question isn't really clear (you should include some of your code!), so I might not answer your question as you hoped. But following is a simple example I have where I join two tables in one query:

private static final String QUERY_ALL_COURSES_WITH_SEMESTER =
 "SELECT * FROM Course JOIN Semester ON semesterId = courseSemesterId";

public Course getCourseByCodeAndSemester(String courseCode, Semester semester)
{
 Course result = null;
 String whereClause = " WHERE courseCode = '" + courseCode.toUpperCase() + "'"
   + " AND semesterId = " + semester.getInternalId();
 Cursor cursor = db.rawQuery(QUERY_ALL_COURSES_WITH_SEMESTER + whereClause, null);
 if (cursor.moveToFirst())
 {
  result = Course.createFromCursor(cursor);
 }
 cursor.close();
 return result;
}

Note that there are probably many things that could be optimized in this function, but it illustrates how it can be done.


As I'm using content providers I ended doing something not quite fast but that works:

themes = managedQuery(
        EventTheme.CONTENT_URI,
        PROJECTIONTHEMES,
        EventTheme.EVENTTYPE + "= ?",
        new String[] {eventType},
        EventTheme.DEFAULT_SORT_ORDER);

String[] from = new String[] { 
        Event._ID,
        Event.NAME,
        Event.STARTDATE,
        Event.ENDDATE,
        PointOfInterest.POINTOFINTERESTNAME
    };

int[] to = new int[] { 
        R.id.event_id,
        R.id.name,
        R.id.start_date,
        R.id.end_date,
        R.id.location_name
    };

while (themes.getPosition() < themes.getCount() - 1) {
    themes.moveToNext();
    eventTheme = themes.getString(themes.getColumnIndexOrThrow(EventTheme._ID));                
    events = managedQuery(
            Event.CONTENT_URI,
            PROJECTIONEVENTS,
            Event.EVENTTHEME + "= ?",
            new String [] { eventTheme } ,
            Event.DEFAULT_SORT_ORDER);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜