Android SQLiteOpenHelper(一)


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(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();
        
    }
}
 
        

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

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

同理,openHelper.getReadableDatabase(); 才是真正的創建、打開數據庫。

 

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

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM