android 读取指定路径数据库文件

    private SQLiteDatabase openDatabase() {
//      String dbFd= "/data"+Environment.getDataDirectory().getAbsolutePath() + "/" + getPackageName()+"/databases/";
      String dbFd= getFilesDir().getAbsolutePath();
      LogTools.i(TAG,"dbFd="+dbFd);
      String dbfile= dbFd+"/test_db";

        try {
            File fd=new File(dbFd);

            if(!fd.exists()){
                fd.mkdirs();
            }

            File file=new File(dbfile);
            if (!file.exists()) {//判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
                
//                InputStream is = getResources().openRawResource(R.raw.countries); //欲导入的数据库
                InputStream is=getAssets().open("test_db");
                FileOutputStream fos = new FileOutputStream(dbfile);
                byte[] buffer = new byte[400000];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }
                fos.close();
                is.close();
            }

            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

            return db;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }

 

你可能感兴趣的:(android,sqlite)