Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if cursor has results

Tags:

android

sqlite

I am using a database to store date and want a single column returned based on which id is given in the where claus.

    public String getPmax(String serial) {

    String[] columns = new String[]{KEY_SERIAL, KEY_PMAX};      
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, "serial = " + serial,
    null, null, null, null);

    String result = "nothing found";

    if(c != null) {

        int iPmax = c.getColumnIndex(KEY_PMAX); 

        c.moveToFirst();
        result = c.getString(iPmax);

    }

    return result;  

}

This code works if I use a serial that is in the database but it gives me a force close when i am using a serial that's not in the database!

it says:

04-11 16:31:19.423: E/AndroidRuntime(21226): java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.janjanus.solardetect/nl.janjanus.solardetect.DatabaseView}: android.database.sqlite.SQLiteException: no such column: g: , while compiling: SELECT serial, pmax FROM SolareCel WHERE serial = g

So my question is: How do I check if there is result? or do I need to check if the serial given is a serial that is in the database?

like image 464
Faas Avatar asked Apr 11 '12 14:04

Faas


1 Answers

    if (!(mCursor.moveToFirst()) || mCursor.getCount() ==0){
     //cursor is empty
}
like image 107
Samir Mangroliya Avatar answered Sep 22 '22 06:09

Samir Mangroliya