解决了。我只是不能接受答案,直到 2 天过去了。原来 sqliteman 毕竟是在读取一个旧数据库。
我正在尝试从 Android 中的数据库中读取数据,但没有得到任何结果。奇怪的是,当我在 sqliteman(一个 sqlite 管理器)中对同一个数据库运行完全相同的查询时,它工作得很好。
我在我的 DB-handler 类中的查询:
//exact same as in code
String fetchVaccinesForCountryQuery =
"SELECT v._id, v.vname, v.text " +
"FROM vaccine v, vacc_disease vc " +
"WHERE v._id = vc.vaccineId and vc.diseaseId IN (SELECT cd.diseaseId " +
"FROM country_disease cd " +
"WHERE cd.countryId = %d)";
//and a version that's a bit easier to read...
String fetchVaccinesForCountryQuery =
"SELECT v._id, v.vname, v.text
FROM vaccine v, vacc_disease vc
WHERE v._id = vc.vaccineId and vc.diseaseId
IN (SELECT cd.diseaseId
FROM country_disease cd
WHERE cd.countryId = %d)";
public Cursor fetchVaccinesForCountry(long countryId) {
String query = String.format(fetchVaccinesForCountryQuery, countryId);
Log.d("TripDb", "fetchStr: " + query);
return mDb.rawQuery(query, null);
}
我编写了一个调用该函数的信息提供程序:
Cursor c = mDb.fetchVaccinesForCountry(countryId);
Log.d("InfoProvider", "numVaccines = " + c.getCount());
显示查询的 logcat 字符串如下所示:
fetchStr: SELECT v._id, v.vname, v.text FROM vaccine v, vacc_disease vc WHERE v._id = vc.vaccineId and vc.diseaseId IN (SELECT cd.diseaseId FROM country_disease cd WHERE cd.countryId = 3)
而 Logcat 显示的数字是
numVaccines = 0
但是当我用 adb 拉数据库并在 SqliteMan 中运行相同的查询时,我得到 7 个结果。我对为什么 Android 没有给我 7 个结果感到困惑。