關於點1)DBHelper
內的以下代碼創建了單例連接(注意,我只關閉主活動的onDestroy
方法中的數據庫)。
/**
* Instantiates a new Db helper.
*
* @param context the context
*/
DBHelper(Context context) {
super(context, DBConstants.DATABASE_NAME, null, 1);
}
private static DBHelper instance;
/**
* Gets helper.
*
* @param context the context
* @return the helper
*/
static synchronized DBHelper getHelper(Context context) {
if(instance == null) {
instance = new DBHelper(context);
}
return instance;
}
和您使用以下方法來得到你的幫助: -
dbhelper = DBHelper.getHelper(context);
db = dbhelper.getWritableDatabase();
關於2使用,db.beginTransaction();
開始交易,db.setTransactionSuccessful();
將其標記爲成功進行數據庫更改後( 這是交易應用所必需的,否則,結束交易將有效抵消應用)和db.endTransaction();
的任何更改以完成交易。 請注意,事務不會嵌套,因此在嵌套事務時,您必須添加代碼,以便beginTransaction
,setTransactionSuccessfull
和endTransaction
僅應用一次。
後續是迎合了嵌套的例子: -
void deleteAisle(long aisleid, boolean intransaction) {
if (doesAisleExist(aisleid)) {
if (!intransaction) {
db.beginTransaction();
}
String whereargs[] = {Long.toString(aisleid)};
// Delete ProductUsage rows that have Aisle as a parent
pudeletes = db.delete(
DBProductusageTableConstants.PRODUCTUSAGE_TABLE,
DBProductusageTableConstants.PRODUCTUSAGE_AISLEREF_COL +
" = ?",
whereargs
);
// More done here but removed for brevity (including deletion of the Aisle)
if (!intransaction) {
db.setTransactionSuccessful();
db.endTransaction();
msg = "DB Transacion SET and ENDED for Aisle ID=" + Long.toString(aisleid);
}
}
}
以上可以單獨調用,但如果要刪除一個店,然後它可以被多次調用,在這種情況下,將帶*號被稱爲*事務**是真實的(所以beginTransaction
,setTransactionSuccessful
和endTransaction
將被跳過,並由父母完成)。
至於實用性,如果一起執行多個操作,則只能使用事務。