SQLite存儲(chǔ)學(xué)習(xí)
1暇检、特色:輕量級(jí)款熬、獨(dú)立深寥、隔離、跨平臺(tái)贤牛、多語言接口惋鹅、安全性
2、數(shù)據(jù)庫的增刪改查
創(chuàng)建數(shù)據(jù)庫:創(chuàng)建一個(gè)SQLiteOpenHelper類
DataBaseHelper兩個(gè)方法:dataBaseHelper.getWritableDatabase();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? dataBaseHelper.getReadableDatabase()
升級(jí)數(shù)據(jù)庫:
增加數(shù)據(jù):
查找:
Content Provider
1殉簸、應(yīng)用程序間共享數(shù)據(jù)的一種方式闰集;為存儲(chǔ)和獲取數(shù)據(jù)提供了統(tǒng)一的接口;Android中為常見的一些數(shù)據(jù)提供了默認(rèn)的Content Provider般卑;四大組件之一武鲁。
適用場(chǎng)景
1) ContentProvider為存儲(chǔ)和讀取數(shù)據(jù)提供了統(tǒng)一的接口
2) 使用ContentProvider,應(yīng)用程序可以實(shí)現(xiàn)數(shù)據(jù)共享
3) android內(nèi)置的許多數(shù)據(jù)都是使用ContentProvider形式蝠检,供開發(fā)者調(diào)用的(如視頻沐鼠,音頻,圖片蝇率,通訊錄等)
當(dāng)應(yīng)用繼承ContentProvider類迟杂,并重寫該類用于提供數(shù)據(jù)和存儲(chǔ)數(shù)據(jù)的方法,就可以向其他應(yīng)用共享其數(shù)據(jù)本慕。雖然使用其他方法也可以對(duì)外共享數(shù)據(jù)排拷,但數(shù)據(jù)訪問方式會(huì)因數(shù)據(jù)存儲(chǔ)的方式而不同,如:采用文件方式對(duì)外共享數(shù)據(jù)锅尘,需要進(jìn)行文件操作讀寫數(shù)據(jù)监氢;采用sharedpreferences共享數(shù)據(jù),需要使用sharedpreferences?API讀寫數(shù)據(jù)藤违。而使用ContentProvider共享數(shù)據(jù)的好處是統(tǒng)一了數(shù)據(jù)訪問方式浪腐。
Uri類簡(jiǎn)介
Uri uri = Uri.parse("content://com.changcheng.provider.contactprovider/contact")
在Content Provider中使用的查詢字符串有別于標(biāo)準(zhǔn)的SQL查詢。很多諸如select, add, delete, modify等操作我們都使用一種特殊的URI來進(jìn)行顿乒,這種URI由3個(gè)部分組成议街, “content://”, 代表數(shù)據(jù)的路徑,和一個(gè)可選的標(biāo)識(shí)數(shù)據(jù)的ID璧榄。以下是一些示例URI:
content://media/internal/images這個(gè)URI將返回設(shè)備上存儲(chǔ)的所有圖片
content://contacts/people/這個(gè)URI將返回設(shè)備上的所有聯(lián)系人信息
content://contacts/people/45這個(gè)URI返回單個(gè)結(jié)果(聯(lián)系人信息中ID為45的聯(lián)系人記錄)
盡管這種查詢字符串格式很常見特漩,但是它看起來還是有點(diǎn)令人迷惑。為此骨杂,Android提供一系列的幫助類(在android.provider包下)涂身,里面包含了很多以類變量形式給出的查詢字符串,這種方式更容易讓我們理解一點(diǎn)搓蚪,因此蛤售,如上面content://contacts/people/45這個(gè)URI就可以寫成如下形式:
Uri person = ContentUris.withAppendedId(People.CONTENT_URI,??45);
然后執(zhí)行數(shù)據(jù)查詢:
Cursor cur = managedQuery(person, null, null, null);
2、Content provider 存取數(shù)據(jù)并使它對(duì)其它應(yīng)用程序可見. 它們是應(yīng)用程序間共享數(shù)據(jù)的唯一方法; 沒有其他的公有數(shù)據(jù)區(qū)域.
3、Content providers為數(shù)據(jù)跨進(jìn)程訪問提供了一套安全的訪問機(jī)制悴能,對(duì)數(shù)據(jù)組織和安全訪問提供了可靠的保證揣钦。
4、通過Content Providers訪問數(shù)據(jù)時(shí)搜骡,在應(yīng)用程序的上下文(Context)中使用ContentResolver對(duì)象最為客戶端(client)與provider進(jìn)行交互拂盯。ContentResolver對(duì)象通過實(shí)現(xiàn)抽象類ContentProvider的一個(gè)實(shí)例來訪問provider。Provider對(duì)象從客戶端(client)接收數(shù)據(jù)請(qǐng)求记靡,執(zhí)行請(qǐng)求操作并且返回請(qǐng)求結(jié)果谈竿。
5、在以下情況下你需要使用Content Providers:
? ? ? a你想為其他應(yīng)用程序提供復(fù)雜數(shù)據(jù)或文件摸吠;
? ? ? b你想允許用戶從你的應(yīng)用程序中拷貝復(fù)雜數(shù)據(jù)到其他的應(yīng)用中
? ? ? c你想使用搜索框架提供自定義的查詢建議功能
6空凸、Content Provider通過URI(統(tǒng)一資源定位符)來訪問數(shù)據(jù),URI可以理解為訪問數(shù)據(jù)的唯一地址寸痢,URI由authority和數(shù)據(jù)地址構(gòu)成呀洲,關(guān)于authority可以理解成網(wǎng)站地址中的主機(jī)地址,而數(shù)據(jù)地址可以理解成某一個(gè)頁面的子地址啼止,二者共同構(gòu)成了一個(gè)完整的訪問地址道逗。對(duì)于authority的命名還是有一定的規(guī)范性的。
7献烦、關(guān)于URI的格式:content:/<authority>/<path>/<id>/滓窍,path就是數(shù)據(jù)路徑,比如說一張表巩那,而id就是這張表中主鍵為id的一行吏夯,也可以理解成一個(gè)實(shí)體對(duì)象。
8即横、實(shí)現(xiàn)抽象類ContentProvider后噪生,有幾個(gè)需要實(shí)現(xiàn)的方法:
Retrieve data from your provider. Use the arguments to select the table to query, the rows and columns to return, and the sort order of the result. Return the data as aCursorobject.
Insert a new row into your provider. Use the arguments to select the destination table and to get the column values to use. Return a content URI for the newly-inserted row.
Update existing rows in your provider. Use the arguments to select the table and rows to update and to get the updated column values. Return the number of rows updated.
Delete rows from your provider. Use the arguments to select the table and the rows to delete. Return the number of rows deleted.
Return the MIME type corresponding to a content URI. This method is described in more detail in the sectionImplementing Content Provider MIME Types.
Initialize your provider. The Android system calls this method immediately after it creates your provider. Notice that your provider is not created until aContentResolverobject tries to access it.
簡(jiǎn)單的總結(jié)一些Content Providers:
1.Content Providers是Android系統(tǒng)中四大組件之一,提供一套在客戶端(client)和數(shù)據(jù)源(data source)之間的訪問接口
2.Content Providers可以提供跨進(jìn)程訪問數(shù)據(jù)的功能东囚,能暴露本地?cái)?shù)據(jù)給其他應(yīng)用訪問
3.實(shí)現(xiàn)Content Providers只需要繼承抽象類ContentProvider并實(shí)現(xiàn)必要的抽象方法即可跺嗽,訪問ContentProvider則根據(jù)URI來訪問