Android SQLiteOpenHelper(一)

來源:互聯網
上載者:User

標籤:cat   class   tap   context   資料庫   inf   href   抽象方法   instance   

SQLiteOpenHelper api解釋:

  • A helper class to manage database creation and version management.   
  • You create a subclass implementing onCreate(android.database.sqlite.SQLiteDatabase), onUpgrade(android.database.sqlite.SQLiteDatabase, int, int) and optionally onOpen(android.database.sqlite.SQLiteDatabase), and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary.Transactions are used to make sure the database is always in a sensible state. 

那這兩大串的英文是什麼意思呢?我們一句一句來翻譯:

  第一句:一個輔助類來管理資料庫建立和版本管理。

        ---->也就是說用於建立、串連、維護資料庫

  第二句:您建立一個子類實現  onCreate(android.database.sqlite.SQLiteDatabase)

               onUpgrade(android.database.sqlite。SQLiteDatabase,int,int)

            和可選  onOpen(android.database.sqlite.SQLiteDatabase)

     而且如果存在這類負責開啟database,如果它不創造,升級是必要的。事務是用來確保資料庫總是在一個合理的狀態。                 

         ---->也就是說:1.用一個類繼承SQLiteOpenHelper

                2.必須實現抽象方法 onCreate() 和onUpgrade() 方法

                3.如果有資料庫,類可以負責開啟資料庫。

                4.如果這個資料庫不能再做創作,升級是必要的。列如:當資料庫表結構要添加一列,使用升級。

                5.這些所有的操作都是為了確保資料庫在一個合理的狀態。列如:新老使用者的、新老版本的資料存放區、添加、修改處理。

由於SQLiteOpenHelper沒有空構造器,繼承的類不能自動調用預設無參構造器,所以必須定義一個顯式建構函式。

比如我們寫一個MySQLiteOpenHelper 繼承SQLiteOpenHelper

public class MySQLiteOpenHelper extends SQLiteOpenHelper{        public MySQLiteOpenHelper(Context context, String name,CursorFactory factory, int version) {        super(context, name, factory, version);    }    @Override    public void onCreate(SQLiteDatabase db) {            }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {            }}

現在,我們看一下這個構造器  MySQLiteOpenHelper(Context context, String name,CursorFactory factory, int version)

  api :

    Create a helper object to create, open, and/or manage a database. The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called. 

  意思是:建立一個helper對象去建立、開啟和/或管理資料庫。資料庫沒有建立或開啟直到getWritableDatabase()或getReadableDatabase()其中一個被調用。

    也就是說:1.建立一個helper   2.helper調用getWritableDatabase()或getReadableDatabase()時才可建立、開啟一個資料庫

  • Context                                       上下文(to use to open or create the database)用於開啟或者建立一個上下文
  • name                                       資料庫名
  • CursorFactory                             遊標(to use for creating cursor objects, or null for the default)null
  • version                                      資料庫版本號碼

四個參數,後三個可以在本類直接聲明。前面我們也瞭解,Activity就是一個上下文。也就是說在Activity調用這個類的時候我們只要傳入一個Activity就可以了。

所以我們修改這個構造器為:  

public class MySQLiteOpenHelper extends SQLiteOpenHelper{        private static final String name = "mydata.db"; // 資料庫名稱    private static final int version = 2; // 資料庫版本        public MySQLiteOpenHelper(Context context) {        super(context, name, null, version);    }    @Override    public void onCreate(SQLiteDatabase db) {            }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {            }}

在Activity中我們就可以這樣用:

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //獲得Helper        MySQLiteOpenHelper openHelper = new MySQLiteOpenHelper(MainActivity.this);  //也可以是 new MySQLiteOpenHelper(getApplicationContext())
    //建立或者開啟資料庫 
    SQLiteDatabase db = openHelper.getReadableDatabase();

    //關閉資料庫
    db.close();
  }
}

可能看到這,有的人就想起了File 檔案。現在我們建立一個java項目 [new -->project-->java project]


public class File1 {        public static void main(String[] args) throws Exception {        //聲明一個File        File file = new File("info.text");             //產生File        FileOutputStream fos = new FileOutputStream(file);        //關閉io        fos.close();            }}

當我們把第11行注釋掉,運行run -->java application ,再把注釋去掉,運行一次

我們發現,new File() 沒有產生一個檔案。這就是我注釋裡寫的:聲明。而 new FileOutputStream() 則是把File產生了。

同理,openHelper.getReadableDatabase(); 才是真正的建立、開啟資料庫。

 

下一篇:Android SQLiteOpenHelper(二) http://www.cnblogs.com/hxb2016/p/6118475.html

 謝謝大家的關注(之前有事太忙)。從今天開始,詩詞就告一段落。來一段時間的經典語錄:世界上唯一不變的,就是一切都在變。

 

Android SQLiteOpenHelper(一)

聯繫我們

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