Search code examples
androidandroid-cursorcursor-positionforceclose

How to return a cursor from database function to another function in activity


I am returning the Cursor from one function in database to another function in activity. Everything working fine but log cat gives the error (which is not causing any problem) to deactivate or close the cursor in called function. If I close it, I can't access it in calling function.I don't know how deactivate works. Also sometime because of force close my database crashes, is it because my cursor is open or because of some other reason?

Called function is in Database class:

public Cursor Settings() {
    mDB = DBHelper.getWritableDatabase();

    Cursor data_db = mDB.rawQuery("SELECT * FROM " + setting + " ;",
            null);

    return data_db;

}

Calling function:

public void insert()
{       
Cursor cr = db.Settings();//db is object of database

            if (cr == null)
                otd.insert_setting(value + "", save_path, option);
            else {
                otd.delete_setting();
                otd.insert_setting(value + "", save_path, option);
            }

}

Solution

  • we should open and close database before and after use of database

    public void insert()
    {   
    db.open();    
    Cursor cr = db.Settings();//db is object of database
    
            if (cr == null)
                otd.insert_setting(value + "", save_path, option);
            else {
                otd.delete_setting();
                otd.insert_setting(value + "", save_path, option);
            }
    db.close
    
    }