我对android中的sqlite数据库有一个新手问题:
每次操作数据时,我真的需要检索可写数据库吗?
那么我可以这样写一个DAO:
class Dao {
private final SQLiteDatabase database;
public Dao(SQLiteOpenHelper databaseHelper){
database = databaseHelper.getWritableDatabase();
}
public void insert(...){
ContentValues cv = new ContentValues(4);
database.insertOrThrow(TABLE, null, cv);
...
}
public void update(...){
ContentValues cv = new ContentValues(4);
database.update(....);
}
}
还是我必须这样写我的道:
class Dao {
private final SQLiteOpenHelper databaseHelper;
public Dao(SQLiteOpenHelper databaseHelper){
this.databaseHelper = databaseHelper
}
public void insert(...){
SQLiteDatabase database = databaseHelper.getWritableDatabase();
ContentValues cv = new ContentValues(4);
database.insertOrThrow(TABLE, null, cv);
...
}
public void update(...){
SQLiteDatabase database = databaseHelper.getWritableDatabase();
ContentValues cv = new ContentValues(4);
database.update(....);
}
}
如果第二种方法是正确的:我是否还需要在每次操作后关闭数据库:
public void update(...){
SQLiteDatabase database = databaseHelper.getWritableDatabase();
ContentValues cv = new ContentValues(4);
database.update(....);
database.close();
}
我想它是一样的databaseHelper.getReadableDatabase()
吧?