Android下SQLite3資料庫操作筆記(二)之-SQLiteOpenHelper

來源:互聯網
上載者:User

在前面使用SQLite3的時候,並沒有留意到有SQLiteOpenHelper這個類,所以只好在Activity裡面去建立和維護資料庫跟資料表的建立。

但是,現在有了SQLiteOpenHelper這個類,就可以把資料庫和資料表,以及一些初始化的資料的維護跟Activity分開了。。。

資料庫和資料表結構的建立,是只需要執行一次的,而開啟資料庫擷取資料庫相應的SQLiteDatabase操作類則有可能是每次運行程式都需要執行的,如何把這兩個步驟操作合理的放到一個輔助類裡面呢?SQLiteOpenHelper!木錯!就是這個類,只需要繼承這個類,調用建構函式SQLiteOpenHelper(Context context, String name, CursorFactory factory,int version)然後重寫onCreate(SQLiteDatabase db)和onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)這兩個方法即可(不過好像我只是使用了onCreate...)。

下面說一下這個類的大概原理,假設你的SQLiteHelper繼承於SQLiteOpenHelper類,調用SQLiteOpenHelper的建構函式並且實現了onCreate和斷onUpgrade,當你在程式中調用getWritableDatabase()方法的時候,會自動去檢查你的databases目錄,如果裡面不存在你需要開啟的資料庫檔案,則會自動調用你所寫的方法onCreate,然後返回你所建立的資料庫表象,如果已經存在則會直接返回該資料庫的表象。這樣,我們初始化的資料庫表,跟預設資料就可以放到onCreate函數裡面去實現。。。

繼承的類建構函式裡面必須調用父類(SQLiteOpenHelper)的建構函式SQLiteOpenHelper(Context context, String name, CursorFactory factory,int version)。

context是為開啟建立資料庫庫用的,name是資料庫的檔案名稱,factory設定為空白使用預設的,version是建立或開啟的資料庫的版本號碼,這個必須大於等於1

如果這一次的version版本和上一次開啟的version不一致的時候,SQLiteOpenHelper就會自動調用onUpgrade方法。。

對了,在Activity中,如果開啟了資料庫,一定要記得關閉!!!


測試SQLiteOpenHelper的一個代碼架構,

SQLiteHelper.java

package com.Yao_GUET.test;import android.content.Context;import android.database.SQLException;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;/** * SQLite3資料庫輔助類 * @author Yao.GUET * blog: http://blog.csdn.net/Yao_GUET * date: 2011-07-06 */public class SQLiteHelper extends SQLiteOpenHelper {private final static String TAG = "SQLiteHelper";public SQLiteHelper(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);// TODO Auto-generated constructor stub}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubLog.e(TAG, "SQLitehelper onCreate!");try {db.execSQL("Create TABLE  Data( " +"ID integer Primary Key AUTOINCREMENT, " +"UserName varchar(50) " +")");Log.e(TAG, "createDataTable OK!");} catch (SQLException se) {se.printStackTrace();}}@Overridepublic void onOpen(SQLiteDatabase db) {// TODO Auto-generated method stubLog.e(TAG, "SQLiteHelper on Open!");super.onOpen(db);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stubLog.e(TAG, "SQLitehelper onUpgrade!");}}

測試Activity

SQLiteHelperTest.java

package com.Yao_GUET.test;import android.app.Activity;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.util.Log;import android.view.KeyEvent;import android.widget.Button;import android.widget.TextView;public class SQLiteHelperTest extends Activity {private final static String TAG = "SQLiteHelperTest";private SQLiteHelper sqlHelper;private SQLiteDatabase db;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.sqlitehelper_test);sqlHelper = new SQLiteHelper(this, "test2.db", null, 2);db = sqlHelper.getWritableDatabase();}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubLog.e(TAG, "onDestroy!");if (db != null)db.close();super.onDestroy();}@Overrideprotected void onPause() {// TODO Auto-generated method stubLog.e(TAG, "onPause");super.onPause();}}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.