Search code examples
androidsqliteopenhelper

Finializing a cursor hasnot deactivated


I have created a function in an application that checks for username and password from the database but i get that error

07-02 00:31:09.619: E/Cursor(345): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/sbn.project.gp/databases/users, table = null, query = SELECT name,password FROM users WHERE name='khaled'and password='1234'

here it is my code

public Boolean check(String nameS, String passwordS) {
        // TODO Auto-generated method stub

        Cursor c = null;
        Boolean check = true;

        c = ourDatabase.rawQuery("SELECT name,password FROM users WHERE name='"
                + nameS + "'" + "and password='" + passwordS + "'", null);

        if (c.getCount() > 0) {
            check = false;
            return check;
        }
        c.close();


        return check;
    }

Solution

  • It's because you didnt close the cursor if check is false , do this instead :

    public Boolean check(String nameS, String passwordS) {
            Cursor c = null;
            Boolean check = true;
    
            c = ourDatabase.rawQuery("SELECT name,password FROM users WHERE name='"
                    + nameS + "'" + "and password='" + passwordS + "'", null);
    
            if (c.getCount() > 0) {
                check = false;
            }
            c.close();
            return check;
        }