Android中應用contentprovider來建立資料庫的一些步驟:

來源:互聯網
上載者:User

1:首先建立一個xxprovider的class,它是extendscontentprovider的。

2:在這個xxprovider中首選定義要建立的資料庫的databasename,以及databaseversion

3:非常重要的是:建立database的authority。值得注意的是這個authority必須和設定檔中的authority必須是一致的

4:建立這個資料庫的要建立的資料表的名字tablename

5:建立外部通過訪問資料表的Uri。(public
static
final Uri
PENDINGLIST_CONTENT_URI = Uri.parse("content://"

          
+ BrowserProvider.AUTHORITY +
"/"

          
+BrowserProvider.PENDINGLIST_TABLE_NAME);)值得注意的是這裡必須是Uri而不是url。這個Uri是由三部分來組成。Content://和authority以及資料表。

6:定義需要建立資料表的一些屬性列。然後可以用一個數組將這些屬性列來封裝起來

7:定義一個UriMacther,它的作用是當有多個資料表的時候,用來匹配當前操作的是哪個資料表的Uri

private
static
final UriMatcher
sUriMatcher;

   
static {

      
sUriMatcher =
new UriMatcher(UriMatcher.NO_MATCH);

      
sUriMatcher.addURI(AUTHORITY,
BOOKMARK_TABLE_NAME,
BOOKMARK_URI);

      
sUriMatcher.addURI(AUTHORITY,
PENDINGLIST_TABLE_NAME,
PENDINGLIST_URI);

      
sUriMatcher.addURI(AUTHORITY,
FOLDER_TABLE_NAME,
FOLDER_URI);

   
}

8:在這個xxprovider中需要定義一個內部類

  
Databasehleper它是繼承了SQLiteOpenHelper,這個內部類有一個建構函式,已經重寫了兩個方法。

DatabaseHelper(Context context){

          
super(context,
DATABASE_NAME,
null,
DATABASE_VERSION);

      
}

@Override

      
public
void onCreate(SQLiteDatabase db) {

 

          
db.execSQL(“建立資料表的語句”

             

);

      
}

 

      
@Override

      
public
void onUpgrade(SQLiteDatabase db,
int oldVersion,
int newVersion) {

          

          
db.execSQL("DROP TABLEIF EXISTS " +
FOLDER_TABLE_NAME);

          
onCreate(db);

      
}

 

9:定義databasehleper這個類的一個對象,用於xxprovider來操作

10:重寫xxprovider的幾個方法

Delete insert oncreate query以及update等方法

 

這些步驟就是如何使用contentprovider來建立資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.