Android 中的关系数据库查询

发布于 2024-09-28 07:17:34 字数 830 浏览 5 评论 0原文

假设我有以下表格,

sqlite> SELECT * FROM Artists;
ArtistID|ArtistName
1       |Peter Gabriel
2       |Bruce Hornsby
3       |Lyle Lovett
4       |Beach Boys
5       |Supernatural

sqlite> SELECT * FROM CDs;
CDID|ArtistID|Title              |Date
1   |1       |So                 |1984
2   |1       |Us                 |1992
3   |2       |The Way It Is      |1986
4   |2       |Scenes from the Southside|1990
5   |1       |Security           |1990
6   |3       |Joshua Judges Ruth |1992
7   |4       |Pet Sounds         |1966

我想要进行这种查询,

sqlite>SELECT ArtistID as _id, ArtistName, Title FROM Artists, CDs WHERE Artists.ArtistID=CDs.ArtistID;  

如何使用 Android SQLiteQueryBuilder 类来使用它,我需要为我的projectionMap 添加什么?

或者仅使用 SQLiteDataBase 类会更容易吗?

suppose I have the following tables

sqlite> SELECT * FROM Artists;
ArtistID|ArtistName
1       |Peter Gabriel
2       |Bruce Hornsby
3       |Lyle Lovett
4       |Beach Boys
5       |Supernatural

sqlite> SELECT * FROM CDs;
CDID|ArtistID|Title              |Date
1   |1       |So                 |1984
2   |1       |Us                 |1992
3   |2       |The Way It Is      |1986
4   |2       |Scenes from the Southside|1990
5   |1       |Security           |1990
6   |3       |Joshua Judges Ruth |1992
7   |4       |Pet Sounds         |1966

I want to make this kind of query

sqlite>SELECT ArtistID as _id, ArtistName, Title FROM Artists, CDs WHERE Artists.ArtistID=CDs.ArtistID;  

How can I use that using the Android SQLiteQueryBuilder class, what do I need to put for my projectionMap?

Or would it be easier just using the SQLiteDataBase class?

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

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

发布评论

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

评论(2

情域 2024-10-05 07:17:34

如何使用 Android SQLiteQueryBuilder 类来使用它

好吧,我只需在 SQLiteDatabase 上使用 rawQuery() 即可,因为您了解 SQL 并且拥有 SQL。

如果有人用枪指着您的头强迫您使用 SQLiteQueryBuilder,您的投影图可能会将 ArtistName 映射到 ArtistNameArtists .ArtistNameTitleTitleCDs.Title。我没有将 SQLiteQueryBuilder 与隐式(或显式)JOIN 一起使用,部分原因是我在编写内容提供程序时仅使用 SQLiteQueryBuilder

How can I use that using the Android SQLiteQueryBuilder class

Well, I'd just use rawQuery() on SQLiteDatabase for that, since you know SQL and have the SQL.

If somebody held a gun to your head to force you to use SQLiteQueryBuilder, your projection map is probably mapping ArtistName to ArtistName or Artists.ArtistName and Title to Title or CDs.Title. I have not used SQLiteQueryBuilder with an implicit (or explicit) JOIN, in part because I only use SQLiteQueryBuilder when I write a content provider.

忘东忘西忘不掉你 2024-10-05 07:17:34

好吧,我想通了。仍然必须对 where 语句进行硬编码。

projectionMap.put(ArtistID, ArtistID + " AS " + _ID);
projectionMap.put(Title, Title);
queryBuilder.setProjectionMap(projectionMap);

Ok I figured it out. Still had to hardcode the where statement.

projectionMap.put(ArtistID, ArtistID + " AS " + _ID);
projectionMap.put(Title, Title);
queryBuilder.setProjectionMap(projectionMap);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文