读取Assets目录下的数据库

来源:互联网 发布:文明5 mac mod文件夹 编辑:程序博客网 时间:2024/05/18 03:29

读取Assets目录下的数据库

有时候在项目中会使用已有的大数据库,我们一般会把已有的数据库放在assets目录下,下面就是如何读取并使用该数据库

  • 把assets下的数据录拷贝到data/data目录下

private void copyDB(String dbName) {

    try {        File file = new File(getFilesDir(), dbName);        if (file.exists() && file.length() > 0) {            // 拷贝成功了,不需要拷贝了        } else {            InputStream is = getAssets().open(dbName);            FileOutputStream fos = new FileOutputStream(file);            byte[] buffer = new byte[1024];            int len = 0;            while ((len = is.read(buffer)) != -1) {                fos.write(buffer, 0, len);            }            fos.close();            is.close();        }    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }}
  • 查询数据库

    SQLiteDatabasedb=SQLiteDatabase.openDatabase(“data/data/xxx/files/”+dbName, null, SQLiteDatabase.OPEN_READONLY);
    Cursor cursor = db.rawQuery(“select * from datable where name=?”, new String[]{“xxx”});

  • -
0 0
原创粉丝点击