我正在执行SQLite查询:
String selectQuery = "SELECT hotel.cityName, hotel.hotelName, hotel.roomName, hotel.roomCode, hotel.roomType, hotel.maxPax FROM hotel WHERE ( hotel.cityName = 'Adelaide' OR hotel.cityName = 'Brisbane' OR hotel.cityName = 'Canberra' ) AND hotel.maxPax < 200 AND hotel.maxPax >= 100 AND ( hotel.roomType = 'Boardroom' )"
针对从XML创建的名为“hotel”的数据库表,其标题如下(根据DDMS):
id (INT)
cityName (TEXT)
hotelName (TEXT)
roomCode (TEXT)
roomName (TEXT)
roomType (TEXT)
maxPax (INT)
每次运行查询时:
SQLiteDatabase db = this.getWritableDatabase ( );
Cursor cursor = db.rawQuery ( selectQuery, null );
cursor.getCount()返回0.
我已检查过实际数据,此查询肯定会返回数据。为什么cursor.getCount()返回0?
答案 0 :(得分:1)
您的Java代码看起来很好,我猜测问题在于您的SQL。当我遇到这样的问题时,我经常在Firefox的SQLite Manager或SQLite Browser中重新创建数据库内容,然后接受查询并开始从WHERE clase中删除内容,直到我得到结果。
答案 1 :(得分:0)
尝试使用'query'方法(Android Developers)
db.query("hotel", null, "(cityName = ? OR cityName = ? OR cityName = ?) AND maxPax < 200 AND maxPax >= 100 AND roomType = ?", new String[] {"Adelaide", "Brisbane", "Canberra", "Boardroom"}, null, null, null);
如果不起作用,请解压缩数据库并测试查询。
答案 2 :(得分:0)
尝试将数据库变量分配给SQLiteOpenHelper。我真的不知道一个相当随意的this
是否会有getWritableDatabase()
方法。
SQLiteOpenHelper helper = new SQLiteOpenHelper()
SQLiteDatabase db = helper.getWritableDatabase();