vendredi 6 mai 2016

Checking if the table has rows results in Null Pointer Exception in SQLiite Android

I want to check if the table has rows and if it contains, then I want to delete it. Below is my code for checking if table has rows in it.

     boolean empty = true;
    Cursor cursor = db.rawQuery("SELECT count(*) FROM TABLE_NAME",null);
    cursor.moveToFirst();
    if (cursor.getInt(0) > 0)
    {
        empty = false;
    }
    else
    {
        empty= true;
    }
    return  empty; 

I even tried checking with below code,

    boolean empty = true;
    Cursor cur = db.rawQuery("SELECT COUNT(*) FROM TABLE_NAME", null);
    if (cur != null && cur.moveToFirst()) {
        empty = (cur.getInt (0) == 0);
    }
    cur.close();

    return empty;    

Both of the above code, throws NullPointerException in line

("SELECT COUNT(*) FROM TABLE_NAME", null);

Any help would be great !! Thanks

Aucun commentaire:

Enregistrer un commentaire