付費下載
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
OpenHelper是安卓打開數(shù)據(jù)庫的幫助類,我們通常需要繼承這個類并重寫里面onCreateonUpgrade方法,因為它是一個抽象類.onCreate方法只會被調(diào)用一次,在數(shù)據(jù)庫創(chuàng)建時被調(diào)用,通常需要在里面做數(shù)據(jù)庫的初始操作,比如說執(zhí)行建表語句等,畢竟一個空的數(shù)據(jù)庫是什么都做不了的.當(dāng)我們需要更新數(shù)據(jù)庫版本時,onUpgrade方法中進(jìn)行更新的操作我們可以通過OpenHelper的getReadableDatabase()和getWritableDatabase()方法來獲取數(shù)據(jù)庫Database對象,通過這個對象對數(shù)據(jù)庫進(jìn)行增刪查改操作.通過方法的名字,我們很容易去認(rèn)為getReadableDatabase()是獲取一個只讀的數(shù)據(jù)庫,但實Createand/oropena Thiswillbethesameobjectreturnedby{@linkunlesssomeproblem,suchasafulldisk,requiresthedatabasetobeopenedread-only.Inthatcase,aread-onlydatabaseobjectwillbeIftheproblemisfixed,afuturecallto{@link#getWritableDatabase}maysucceed,inwhichcasetheread-onlydatabaseobjectwillbeclosedandtheread/writeobjectwillbereturnedinthefuture.Like{@link#getWritableDatabase},thismethodmaytakealongtimetoreturn,soyoushouldnotcallitfromtheapplicationmainthread,includingfrom{@linkandroid.content.ContentProvider#onCreate Exceptionifthedatabasecannotbe@returnadatabaseobjectvaliduntil{@link#getWritableDatabase}or{@link#close}iscalled.用(getReadableDatabase()getWritableDatabase(),先調(diào)用的數(shù)據(jù)庫 DatabasegetReadableDatabase()synchronized(this)return}}Createand/oropenadatabasethatwillbeusedforreadingandThefirsttimethisiscalled,thedatabasewillbeopened{@link#onCreate},{@link#onUpgrade}and/or{@link#onOpen}willbeonCreateonUpgrade和/(如果打開成功,onOpen Onceopenedsuccessfully,thedatabaseiscached,soyoucancallthismethodeverytimeyouneedtowritetothedatabase.(Makesuretocall{@link#close}whenyounolongerneedtheErrorssuchasbadpermissionsorafulldiskmaycausethismethodtofail,butfutureattemptsmaysucceediftheproblemisfixed.Databaseupgrademaytakealongtime,youshouldnotcallthismethodfromtheapplicationmainthread,includingfrom{@linkandroid.content.ContentProvider#onCreateContentProvider.onCreate()}. Exceptionifthedatabasecannotbeopenedforwriting@returnaread/writedatabaseobjectvaliduntil{@link#close}is DatabasegetWritableDatabase()synchronized(this)return}}通過上面的注釋和代碼,我們可以知道getReadableDatabase()的數(shù)據(jù)庫時才會打開一個只讀的數(shù)據(jù)庫,這兩個方法內(nèi)部實現(xiàn)基本相同,都調(diào)用了這樣的一個方法:getDatabaseLocked(booleanwritable),他們是通過這個方法的參數(shù)來進(jìn)行區(qū)分的,getReadableDatabase()方法傳入的參數(shù)是false,而getWritableDatabase()方法傳入的參數(shù)是DatabasegetDatabaseLocked(booleanwritable)DatabasegetDatabaseLocked(booleanwritable)if(mDatabase!=null)//mDatabase如果不等于null,說明之前已經(jīng)創(chuàng)建過數(shù)據(jù)庫,也就是說 ,獲取的數(shù)據(jù)庫對象會被緩存if(!mDatabase.isOpen())//Darn!Theuserclosedthedatabasebycalling//open狀態(tài)的也就是說調(diào)用過close()方法mDatabase=null;}elseif(!writable||!mDatabase.isReadOnly())//Thedatabaseisalreadyopenfor//elseif說明數(shù)據(jù)庫可用,writablefalse,getReadableDatabase()方法那么直接返回該數(shù)據(jù)庫對象因為該數(shù)據(jù)庫對象既然可用,那么無論它是只讀還是可讀可寫,都可以讀,就可以在這里直接返回getWritableDatabase()方法那么只有該數(shù)據(jù)庫對象不是只讀的才可以返回,否則就要新創(chuàng)建一個.return}}if(mIsInitializing)thrownewIllegalStateException("getDatabasecalled}Databasedb= 1.db=trymIsInitializing=if(db!=null)if(writable&&db.isReadOnly()){}}elseif(mName==null)Databasecreate()方法創(chuàng)建一個數(shù)據(jù)庫CursorFactorynull,該db }elsetryif(DEBUG_STRICT_READONLY&&!writable)//如果是OpenHelperDEBUG_STRICT_READONLY并且調(diào)getReadableDatabase().,寫數(shù)據(jù)庫失敗true也就是如果之前創(chuàng)建的是一個只讀的數(shù)finalStringpath=db= Database.openDatabase(path,mFactory,}elsedb=mContext.openOrCreateDatabase(mName,mEnableWriteAheadLogging?Context.MODE_ENABLE_WRITE_AHEAD_LOGGING:0,mFactory,}}catch Exceptionex)if(writable)(getReadableDatabase()數(shù)據(jù)庫創(chuàng)建失敗那么應(yīng)該嘗試創(chuàng)建只讀的數(shù)據(jù)throw}//走到這里說明創(chuàng)建數(shù)據(jù)庫失敗而且調(diào)用的是getReadableBase(),Log.e(TAG,"Couldn'topen"+mName+"forwriting(willtryread-only):",finalStringpath=db Database.openDatabase(path,Database.OPEN_READONLY,}}//db的配置如果走到這里db創(chuàng)建成功那么就可以調(diào)用這個方法finalintversion=if(version!=mNewVersion)if(db.isReadOnly())thrownew Exception("Can'tupgraderead-onlydatabasefromversion"+db.getVersion()+"to"+mNewVersion+":+}//開啟事務(wù),這里表明了 OpenHelper的onCreate(),onUpgrade()方法是在事務(wù)中進(jìn)行的,也就是不必?fù)?dān)心這兩個方法中的代碼只有部分被執(zhí)行.tryif(version==0)}elseif(version>mNewVersion)onDowngrade(db,version,}else}}
onUpgrade(db,version,
}finally}}//onOpen方法因為數(shù)據(jù)庫剛創(chuàng)建open的所以可以在這里調(diào)用這個方if(db.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年廣東省外語藝術(shù)職業(yè)學(xué)院單招職業(yè)適應(yīng)性測試題庫附答案解析
- 2025年廣豐縣幼兒園教師招教考試備考題庫及答案解析(奪冠)
- 2025年江西管理職業(yè)學(xué)院馬克思主義基本原理概論期末考試模擬題帶答案解析
- 2025年北京網(wǎng)絡(luò)職業(yè)學(xué)院馬克思主義基本原理概論期末考試模擬題帶答案解析(必刷)
- 2025年昆山登云科技職業(yè)學(xué)院馬克思主義基本原理概論期末考試模擬題含答案解析(奪冠)
- 2025年四川工程職業(yè)技術(shù)大學(xué)馬克思主義基本原理概論期末考試模擬題帶答案解析(奪冠)
- 2025年懷化師范高等專科學(xué)校單招職業(yè)適應(yīng)性考試題庫帶答案解析
- 2025年木壘縣幼兒園教師招教考試備考題庫帶答案解析(必刷)
- 2025年黑龍江農(nóng)業(yè)職業(yè)技術(shù)學(xué)院馬克思主義基本原理概論期末考試模擬題帶答案解析
- 2025年昌黎縣幼兒園教師招教考試備考題庫含答案解析(必刷)
- 滬教版初中英語七年級下冊單詞匯表
- 反向開票協(xié)議書
- 林場管護合同范例
- 春節(jié)后收心培訓(xùn)
- 福建省福州市2023-2024學(xué)年高一上學(xué)期期末質(zhì)量檢測英語試題 含答案
- 二次結(jié)構(gòu)承包合同
- GB/T 44592-2024紅樹林生態(tài)保護修復(fù)技術(shù)規(guī)程
- GB/T 43851-2024制造物流系統(tǒng)互聯(lián)互通通用要求
- 直播運營指南(從主播修煉、平臺運營到商業(yè)獲利)
- 《樹立正確的政績觀》課件
- 產(chǎn)品制造可行性評估報告
評論
0/150
提交評論