user2313303
user2313303

Reputation: 3

SQLite Query not returning any records

I'm executing an SQLite query:

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

against a database table called "hotel" created from XML, with the following headings (according to DDMS):

id (INT)
cityName (TEXT)
hotelName (TEXT)
roomCode (TEXT)
roomName (TEXT)
roomType (TEXT)
maxPax (INT)

Each time I run the query:

    SQLiteDatabase db = this.getWritableDatabase ( );
    Cursor cursor = db.rawQuery ( selectQuery, null );

cursor.getCount() returns 0.

I have checked the actual data, and this query should definitely return data. Why does cursor.getCount() return 0?

Upvotes: 0

Views: 882

Answers (3)

kenny_k
kenny_k

Reputation: 3980

Your Java code looks fine, I am guessing the problem is with your SQL. When I have such problems, I often re-create the DB contents in Firefox's SQLite Manager or SQLite Browser and then take the query and start removing things from the WHERE clase until I get results.

Upvotes: 1

StoneBird
StoneBird

Reputation: 1940

Try assign your database variable to a SQLiteOpenHelper. I don't really know if a rather arbitrary this would have getWritableDatabase() method.

SQLiteOpenHelper helper = new SQLiteOpenHelper()
SQLiteDatabase db = helper.getWritableDatabase();

Upvotes: 0

viniciuskneves
viniciuskneves

Reputation: 80

Try to use the 'query' method (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);

If it doesn't work, extract the database and test the query.

Upvotes: 0

Related Questions