db生成方法一:将assets中的数据库atf.db拷贝到内部存储中

来源:互联网 发布:在淘宝退款卖家不同意 编辑:程序博客网 时间:2024/05/01 21:44


public class DBManager {       private SQLiteDatabase db;    Context context;    public static final String DB_NAME = "atf.db";     public DBManager(Context context){        this.context=context;    db=DBManagerS("com.cosber.wse.atf");    }

//把assets目录下的db文件复制到dbpath下public SQLiteDatabase DBManagerS(String packName) {    String dbPath = "/data/data/" + packName            + "/databases/" + DB_NAME;    if (!new File(dbPath).exists()) {        try {            FileOutputStream out = new FileOutputStream(dbPath);            InputStream in = context.getAssets().open(DB_NAME);            byte[] buffer = new byte[1024];            int readBytes = 0;            while ((readBytes = in.read(buffer)) != -1)                out.write(buffer, 0, readBytes);            in.close();            out.close();        } catch (IOException e) {            e.printStackTrace();        }    }    return SQLiteDatabase.openOrCreateDatabase(dbPath, null);}
}

原创粉丝点击