如何使用 ManagedQuery 连接多个表?

发布于 2024-10-05 04:44:10 字数 325 浏览 2 评论 0原文

我正在尝试进行查询,从两个表中获取数据,但它不会向我显示任何结果。我知道有一个结果,因为在 SQLite3 中它至少显示一个结果,例如

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从此见与不见 2024-10-12 04:44:10

您的问题不太清楚(您应该包含一些代码!),所以我可能不会按照您希望的方式回答您的问题。但以下是我在一个查询中连接两个表的一个简单示例:

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;
}

请注意,此函数中可能有很多可以优化的内容,但它说明了如何完成此操作。

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.

仙气飘飘 2024-10-12 04:44:10

当我使用内容提供商时,我最终做了一些不太快但有效的事情:

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文