«

Android导入现有的数据库

时间:2024-3-2 19:53     作者:韩俊     分类: Android


常用的路径写法

获取/system路径

Environment.getRootDirectory() ;

获取/cache路径

Environment.getDownloadCacheDirectory();

获取当前程序路径
context.getFilesDir().getAbsolutePath();

获取该程序的安装包路径

context.getPackageResourcePath();

获取程序默认数据库路径

context.getDatabasePath("city.db").getAbsolutePath();

内部存储路径:/data/data/youPackageName/,应用卸载时会被删除

"/data"+Environment.getDataDirectory().getAbsolutePath()+"youPackageName";

SD卡的跟路径

Environment.getExternalStorageDirectory().getAbsolutePath();

导入数据库

DBManager.java

public class DBManager {
    public static final String DB_NAME = "city.db";
    public static final String PACKAGE = "com.android.test";
    public static final String DB_PATH = "/data"+Environment.getDataDirectory().getAbsolutePath()+"/"+PACKAGE;

    private SQLiteDatabase database;
    private Context context;

    DBManager(Context context) {
        this.context = context;
    }

    public void openDatabase() {
        this.database = openDatabase(DB_PATH + "/" + DB_NAME);
    }

    private SQLiteDatabase openDatabase(String dbfile) {
        AssetManager assetManager = context.getAssets();
        File file = new File(dbfile);
        SharePreferenceUtil mSpUtil = SharePreferenceUtil.getSharePreferenceUtil(context);
        int version = mSpUtil.getVersion();
        try {
            //如果不存在或者第一次运行执行拷贝
            if (!file.exists() || version < 1) {
                //有两种方式可以读取
                //InputStream is = context.getResources().openRawResource(R.raw.city);
                InputStream is = assetManager.open(DB_NAME);
                FileOutputStream fos = new FileOutputStream(dbfile);
                byte[] buffer = new byte[1024];
                int length = -1;
                while ((length = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, length);
                    fos.flush();
                }
                fos.close();
                is.close();
                mSpUtil.setVersion(1);
            }
            //打开database
            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,null);
            return db;
        } catch (FileNotFoundException e) {
            Log.e("Database", "File not found");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("Database", "IO exception");
            e.printStackTrace();
        }
        return null;
    }

    public void closeDatabase() {
        this.database.close();
    }
}

SharePreferenceUtil.java

public class SharePreferenceUtil {
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private static SharePreferenceUtil mSpUtil;
    private static final String VERSION = "version";

    public SharePreferenceUtil(Context context, String file) {
        sp = context.getSharedPreferences(file, Context.MODE_PRIVATE);
        editor = sp.edit();
    }
    public static SharePreferenceUtil getSharePreferenceUtil(Context context) {
        if (mSpUtil == null)
            mSpUtil = new SharePreferenceUtil(context,"city");
        return mSpUtil;
    }

    public int getVersion() {
        return sp.getInt(VERSION, -1);
    }

    public void setVersion(int version) {
        editor.putInt(VERSION, version);
        editor.commit();
    }
}

上面的例子是将现有的数据库city.db导入到内部存储路径/data/data/com.android.test/下,其中有两种获取city.db的方式;

1、city.db放在assets目录下,直接通过context.getAssets().open("city.db")获取;

2、city.db放在res下的raw文件夹中,通过context.getResources().openRawResource(R.raw.city)获取;

两者区别:assets下的文件不会被编译,也就是说在R文件中无法找到,而raw文件夹中的文件会被编译,在R文件中能找到,因此可以使用R.raw.city去获取;

一般情况下像配置文件、数据量比较小的数据库文件在导入时可以导入到内部存储路径,当程序被卸载的时候会跟着被删除,一些较大的文件或动态的文件,比如聊天数据一般情况下可导入SD卡路径下自己生成一个文件夹中;



如需转载请注明出处:http://blog.csdn.net/zapperbot


        <p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>

标签: android

热门推荐