標籤: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代碼
- /**
- *
- */
- package com.yourpackage;
-
- import java.io.BufferedReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
-
- /**
- * @author fan.zhang
- *
- */
- public class DataBaseHelper extends SQLiteOpenHelper {
- private static String DB_PATH = "/data/data/your_package_name/databases/";
- private static final String DB_NAME = "your_db_name.db";
-
- private SQLiteDatabase myDataBase;
-
- private final Context myContext;
-
- public FileSQLiteHelp(Context context) {
- super(context, DB_NAME, null, 1);
- this.myContext = context;
- }
-
- /**
- * inital your database from your local res-raw-folder to the just created
- * empty database in the system folder, from where it can be accessed and
- * handled.
- * */
- private void initDataBase()throws IOException {
-
- // Open your local db as the input stream
- // InputStream myInput = myContext.getAssets().open(DB_NAME);
- InputStream myInput = myContext.getResources().openRawResource(
- R.raw.your_db_file_name);
- InputStreamReader reader = new InputStreamReader(myInput);
- BufferedReader breader = new BufferedReader(reader);
-
- // Path to the just created empty db
- String outFileName = DB_PATH + DB_NAME,
- str;
-
- // Open the empty db as the output stream
- FileWriter myOutput = new FileWriter(outFileName, true);
- while ((str = breader.readLine()) != null) {
- myDataBase.execSQL(str); //exec your SQL line by line.
- }
-
- // Close the streams
- myOutput.flush();
- myOutput.close();
- myInput.close();
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
- * .SQLiteDatabase)
- */
- @Override
- public void onCreate(SQLiteDatabase db) {
- // TODO Auto-generated method stub
- myDataBase = db;
-
- try {
- this.initDataBase();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
-
- @Override
- public void onOpen(SQLiteDatabase db) {
- boolean readOnly = db.isReadOnly();
- }
- /*
- * (non-Javadoc)
- *
- * @see
- * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
- * .SQLiteDatabase, int, int)
- */
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // TODO Auto-generated method stub
-
- }
- }
可以按正常邏輯用DataBaseHelper.getReadableDatabase()來使用你的資料庫了。
Android中用檔案初始化sqlite 資料庫(二)