gpt4 book ai didi

android - 我必须关闭 SQLiteOpenHelper 对象吗?

转载 作者:行者123 更新时间:2023-11-29 21:26:42 24 4
gpt4 key购买 nike

我创建了一个 SQLiteOpenHelper 对象,运行 dBMainHelper.EditRuleMain(rule) 后是否必须关闭 dBMainHelper 以节省资源?

如果不运行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();
}


}

最佳答案

是的,你应该明确地调用 close。

检查这些链接 When should I call close() on SQLiteOpenHelper used by ContentProvider

Android Database Size Wont Decrease On HTC Thunderbolt

读清楚第二个

关于android - 我必须关闭 SQLiteOpenHelper 对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20184553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com