Home > DeveloperSection > Forums > Using the WHERE clause in SQLite
Ankita Pandey
Ankita Pandey

Total Post:183

Posted on    May-29-2013 11:52 PM

 Android Android  Sqlite  Sqlite3 

 1 Reply(s)
 1934  View(s)
Rate this:
Hi Expert!

I have a general question about using WHERE clauses in SQLite. I have worked with SQLite some and know my way around. However, I'm having trouble with WHERE clauses.

I have an Android application in which I'm needing to do a few simple operations with the SQLite WHERE clause. However, despite experimenting with the WHERE clause 

all day, I've come up empty. I am trying to perform very simple SQLite commands (e.g. SELECT name FROM tablename WHERE _id=2 or DELETE FROM tablename WHERE 

name='somename'). But, every time I use the where clause I either get 0 rows returned (in the case of a SELECT) or have 0 rows deleted.

The table name is correct. The column names are correct. (I haven't had any trouble selecting or inserting as long as I don't specify a WHERE clause.) I made sure 

that the queries/statements were well formed. I've tried both raw queries/statements as well as the methods (I made sure to use the correct methods for SELECT and 

DELETE {e.g. rawQuery() or query() for SELECT}) provided from the SQLiteDatabase class in Android, but nothing has worked.

All I need is to be able to perform simple queries/statements using the WHERE clause. Does anyone have any insight as to what's happening?

Thanks in advance! 


Total Post:604

Posted on    May-30-2013 5:36 AM

Hi Ankita!

You can try as following

Method First:

String sqlQuery = "select * from Appointment where start_date='"
                + startDate + "' order by start_date,time(start_date) ASC";

        cursor = getReadableDatabase().rawQuery(sqlQuery, null);
        // Log.e("Appointment ->","Total rows fetched for date "+
        // startDate+" "+cursor.getCount());
        if (cursor != null && cursor.moveToFirst()) {
            do {

            } while (cursor.moveToNext());

Method Second:

private boolean isRequestExists(int requestId) {
    Cursor cursor = null;
    boolean result = false;
    try {
        String[] args = { "" + requestId };
        StringBuffer sbQuery = new StringBuffer("SELECT * from ").append(
                TABLE_NAME).append(" where _id=?");
        cursor = getReadableDatabase().rawQuery(sbQuery.toString(), args);
        if (cursor != null && cursor.moveToFirst()) {
            result = true;
    } catch (Exception e) {
        Log.e("Requestdbhelper", e.toString());
    return result;

I hope this code might be resolve your problem. 

Don't want to miss updates? Please click the below button!

Follow MindStick