从Android数据库中读取时没有结果

时间:2013-01-05 22:56:17

标签: android sqlite

解决。直到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个结果。

2 个答案:

答案 0 :(得分:0)

我认为代码中唯一的问题可能是您在定义查询字符串时没有注意换行符。我建议你在一行中定义你的查询,或者这样做:

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

答案 1 :(得分:0)

所以...这有点令人尴尬。事实证明,当我认为SqliteMan重新加载数据库时,它使用的是旧文件。一旦我发现我可以开始寻找真正的错误,现在一切正常......