Android 怎樣使用已存在的Database

來源:互聯網
上載者:User

    private SQLiteDatabase db;
    private DatabaseHelper dbHelper;
    private Context context;

    public DBAdapter(Context ctx) {
        this.context = ctx;
        dbHelper = new DatabaseHelper(context);
    }

    private class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, Version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // create table
            Log.i("onCreateTable",":");
            db.execSQL(Category_CREATE);
            db.execSQL(FAVORITE_CREATE);
            db.execSQL(Message_CREATE);
            db.execSQL(Key_Mess_CREATE);
            db.execSQL(W_Message_LIST_CREATE);
            db.execSQL(W_Message_CREATE);   
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("drop table if exists "+Category_TABLE);   
            db.execSQL("drop table if exists "+FAVORITE_TABLE);   
            db.execSQL("drop table if exists "+Message_TABLE);   
            db.execSQL("drop table if exists "+Key_Mess_TABLE);   
            db.execSQL("drop table if exists "+W_Message_TABLE);   
            db.execSQL("drop table if exists "+W_Message_LIST);   
            onCreate(db);
        }

    }
    public DBAdapter open() {
        //db = context.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
        try
        {
            // 獲得dictionary.db檔案的絕對路徑
            String databaseFilename = DATABASE_PATH + "/" + DATABASE_NAME;
            File dir = new File(DATABASE_PATH);
            // 如果/sdcard/dictionary目錄中存在,建立這個目錄
            if (!dir.exists())
                dir.mkdir();
            // 如果在/sdcard/dictionary目錄中不存在
            // dictionary.db檔案,則從res/raw目錄中複製這個檔案到
            // SD卡的目錄(/sdcard/dictionary)
            File file = new File( DATABASE_PATH + "/" + DATABASE_NAME_OLD);
            if(file.exists()){
                Log.i(databaseFilename,"delete db");
                file.delete();
            }
            if (!(new File(databaseFilename)).exists())
            {   
                Log.i(databaseFilename,"copy db");
                // 獲得封裝dictionary.db檔案的InputStream對象
                InputStream is = context.getResources().openRawResource(
                        R.raw.wpp);
                FileOutputStream fos = new FileOutputStream(databaseFilename);
                byte[] buffer = new byte[8192];
                int count = 0;
                // 開始複製dictionary.db檔案
                while ((count = is.read(buffer)) > 0)
                {
                    fos.write(buffer, 0, count);
                }

                fos.close();
                is.close();
            }
            // 開啟/sdcard/dictionary目錄中的dictionary.db檔案
                db = SQLiteDatabase.openOrCreateDatabase(
                    databaseFilename, null);
            return this;
        }
        catch (Exception e)
        {
        }
        return null;
    }

    public void close() {
        try {
            dbHelper.close();
        } catch (Exception e) {

        }

    }

聯繫我們

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