Android 怎样使用已存在的Database

    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) {

        }

    }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值