Android 中的关系数据库查询
假设我有以下表格,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我只需在
SQLiteDatabase
上使用rawQuery()
即可,因为您了解 SQL 并且拥有 SQL。如果有人用枪指着您的头强迫您使用
SQLiteQueryBuilder
,您的投影图可能会将ArtistName
映射到ArtistName
或Artists .ArtistName
和Title
为Title
或CDs.Title
。我没有将SQLiteQueryBuilder
与隐式(或显式)JOIN
一起使用,部分原因是我在编写内容提供程序时仅使用SQLiteQueryBuilder
。Well, I'd just use
rawQuery()
onSQLiteDatabase
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 mappingArtistName
toArtistName
orArtists.ArtistName
andTitle
toTitle
orCDs.Title
. I have not usedSQLiteQueryBuilder
with an implicit (or explicit)JOIN
, in part because I only useSQLiteQueryBuilder
when I write a content provider.好吧,我想通了。仍然必须对 where 语句进行硬编码。
Ok I figured it out. Still had to hardcode the where statement.