HelloCW
HelloCW

Reputation: 2255

Must I close SQLiteOpenHelper object?

I create an object of SQLiteOpenHelper, must I close dBMainHelper after I run dBMainHelper.EditRuleMain(rule) to save resource?

Does the system will free resource automatically if I don't run dBMainHelper.close()?

    public static void EditRuleMain(MRule rule) {
        RuleMain dBMainHelper=new RuleMain(PublicPar.myContext);
        dBMainHelper.EditRuleMain(rule);
                 //Must I close?        
        dBMainHelper.close();
    }




public class RuleMain  extends SQLiteOpenHelper {

    private final static String DBName="smsforwardrulemain.db";
    private final static String TableRuleMain="rulemain";     

    public RuleMain(Context context) {
        super(context, DBName, null, DBPublicPar.DBVersion);  
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String sql = "Create table " 
                + TableRuleMain
                + " (ruleID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
                + "enabled INTEGER NOT NULL," 
                + "incomingType INTEGER NOT NULL," 
                + "name TEXT NOT NULL" 
                + ");";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        String sql=" DROP TABLE IF EXISTS "+TableRuleMain;
        db.execSQL(sql); 
        onCreate(db);
    }   



    private List<MRule> GetMRuleListFromCursor(Cursor cursor) {
        List<MRule> myRuleMainList = new ArrayList<MRule>();
        while (cursor.moveToNext()) {
            MRule myMRule = new MRule();
            myMRule.ruleID = cursor.getInt(cursor.getColumnIndex("ruleID"));
            myMRule.name = cursor.getString(cursor.getColumnIndex("name"));
            myMRule.enabled = (cursor.getInt(cursor.getColumnIndex("enabled")) == 0) ? false: true;
            myMRule.incomingType= IncomingType.values()[cursor.getInt(cursor.getColumnIndex("incomingType"))];
            myRuleMainList.add(myMRule);
        }
        return myRuleMainList;
    }


    public int AddRuleMain(MRule rule) {        
        ContentValues cv=new ContentValues(); 
        FillRuleMainVaule( rule,cv);

        SQLiteDatabase db = this.getWritableDatabase(); 
        long rowid=db.insert(TableRuleMain, null, cv);
        db.close();     

        return (int)rowid;
    }

    public void EditRuleMain( MRule rule) {
        ContentValues cv=new ContentValues(); 
        FillRuleMainVaule( rule,cv);    

        SQLiteDatabase db = this.getWritableDatabase();
        db.update(TableRuleMain, cv, "ruleID = ?", new String[]{String.valueOf(rule.ruleID)});  
        db.close();
    }


}

Upvotes: 1

Views: 263

Answers (1)

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

Yes you should call close explicitly.

Check these links When should I call close() on SQLiteOpenHelper used by ContentProvider

or

Android Database Size Wont Decrease On HTC Thunderbolt

read the 2nd one clearly

Upvotes: 2

Related Questions