我很新,正在尝试学习如何在 android 中使用 sqlite 使用 Select、Join 和 Where。理想情况下,这就是我想从数据库返回的内容:
角色 ID 为 4(远程)的人员:
select people.[name]
from people
join people_role
on people.[id] = people_role.[people_id]
where role_id = 4
返回 role_id 为 4 的 People 的代码是什么?
1- 使用SQLiteBrowser创建您的数据库。
2-将数据库保存在资产文件夹中
3-按照这个解决方案。
4- 在 TestAdapter 类中找到getTestData()并将其替换为:
public Cursor getTestData()
{
try
{
String sql ="select people.[name]
from people
join people_role
on people.[id] = people_role.[people_id]
where role_id = 4 ";
Cursor mCur = mDb.rawQuery(sql, null);
if (mCur!=null)
{
mCur.moveToNext();
}
return mCur;
}
catch (SQLException mSQLException)
{
Log.e(TAG, "getTestData >>"+ mSQLException.toString());
throw mSQLException;
}
}