Android中用檔案初始化sqlite 資料庫(二)

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   os   使用   sp   java   

  androidsqlite啟動時資料庫初始化 

方法1已經講述了一種初始化資料庫的方法

它的資料庫初始化不是用sql語句,而是用一個現成的sqlite的二進位檔案進行直接copy到Android系統的資料庫路徑中。我一方面不太放心這種二進位檔案的直接copy,另一方面,如果sqlite資料庫的二進位結構有所升級或變動,我的程式就無法對所有sqlite版本相容了。

 

方法2:在啟動時,執行sql檔案進行資料庫初始化.

SQLiteOpenHelper的onCreate僅在程式第一次使用資料庫時執行,然後執行onCreate;此後的執行就會跳過onCreate,直接執行onOpen。因此完全無需自己去檢測資料庫是否最在.

以下按照SQLiteOpenHelper的”正確“使用方法,本人寫的SQLiteOpenHelper的子類,你可以用它初始你的Android資料庫。資料庫用sql語句形式,放在項目的res / raw目錄底下,記住每行一條語句(因為我用readline()來讀源檔案),不能換行!你可以用本機資料庫匯出工具,此處省略資料匯出。

代碼如下:

Java代碼  
  1. /** 
  2.  * 
  3.  */  
  4. package com.yourpackage;  
  5.   
  6. import java.io.BufferedReader;  
  7. import java.io.FileWriter;  
  8. import java.io.IOException;  
  9. import java.io.InputStream;  
  10. import java.io.InputStreamReader;  
  11. import android.content.Context;  
  12. import android.database.sqlite.SQLiteDatabase;  
  13. import android.database.sqlite.SQLiteOpenHelper;  
  14.   
  15. /** 
  16.  * @author fan.zhang 
  17.  * 
  18.  */  
  19. public class DataBaseHelper extends SQLiteOpenHelper {  
  20.     private static String DB_PATH = "/data/data/your_package_name/databases/";  
  21.     private static final String DB_NAME = "your_db_name.db";  
  22.   
  23.     private SQLiteDatabase myDataBase;  
  24.   
  25.     private final Context myContext;  
  26.   
  27.     public FileSQLiteHelp(Context context) {  
  28.         super(context, DB_NAME, null, 1);  
  29.         this.myContext = context;  
  30.     }  
  31.   
  32.     /** 
  33.      * inital your database from your local res-raw-folder to the just created 
  34.      * empty database in the system folder, from where it can be accessed and 
  35.      * handled. 
  36.      * */  
  37.     private void initDataBase()throws IOException {  
  38.   
  39.         // Open your local db as the input stream  
  40.         // InputStream myInput = myContext.getAssets().open(DB_NAME);  
  41.         InputStream myInput = myContext.getResources().openRawResource(  
  42.                 R.raw.your_db_file_name);  
  43.         InputStreamReader reader = new InputStreamReader(myInput);  
  44.         BufferedReader breader = new BufferedReader(reader);  
  45.   
  46.         // Path to the just created empty db  
  47.         String outFileName = DB_PATH + DB_NAME,  
  48.         str;  
  49.   
  50.         // Open the empty db as the output stream  
  51.         FileWriter myOutput = new FileWriter(outFileName, true);  
  52.         while ((str = breader.readLine()) != null) {  
  53.             myDataBase.execSQL(str); //exec your SQL line by line.  
  54.         }  
  55.   
  56.         // Close the streams  
  57.         myOutput.flush();  
  58.         myOutput.close();  
  59.         myInput.close();  
  60.   
  61.     }  
  62.   
  63.     /* 
  64.      * (non-Javadoc) 
  65.      * 
  66.      * @see 
  67.      * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite 
  68.      * .SQLiteDatabase) 
  69.      */  
  70.     @Override  
  71.     public void onCreate(SQLiteDatabase db) {  
  72.         // TODO Auto-generated method stub  
  73.         myDataBase = db;  
  74.   
  75.         try {  
  76.             this.initDataBase();  
  77.         } catch (IOException e) {  
  78.             // TODO Auto-generated catch block  
  79.             e.printStackTrace();  
  80.         }  
  81.   
  82.     }  
  83.   
  84.     @Override  
  85.     public void onOpen(SQLiteDatabase db) {  
  86.         boolean readOnly = db.isReadOnly();  
  87.     }  
  88.     /* 
  89.      * (non-Javadoc) 
  90.      * 
  91.      * @see 
  92.      * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite 
  93.      * .SQLiteDatabase, int, int) 
  94.      */  
  95.     @Override  
  96.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  97.         // TODO Auto-generated method stub  
  98.   
  99.     }  
  100. }  

可以按正常邏輯用DataBaseHelper.getReadableDatabase()來使用你的資料庫了。

Android中用檔案初始化sqlite 資料庫(二)

聯繫我們

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