栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

无法从资产复制SQLite数据库

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无法从资产复制SQLite数据库

我使用此助手,并且工作正常:

public class DBHelper extends SQLiteOpenHelper{private final static String DB_PATH = "/data/data/[YOUR PACKAGE HERE]/databases/";String dbName;Context context;File dbFile;public DBHelper(Context context, String dbName, CursorFactory factory,        int version) {    super(context, dbName, factory, version);    this.context = context;    this.dbName = dbName;    dbFile= new File(DB_PATH + dbName);}@Overridepublic synchronized SQLiteDatabase getWritableDatabase() {    if(!dbFile.exists()){        SQLiteDatabase db = super.getWritableDatabase();        copyDatabase(db.getPath());    }    return super.getWritableDatabase();}@Overridepublic synchronized SQLiteDatabase getReadableDatabase() {    if(!dbFile.exists()){        SQLiteDatabase db = super.getReadableDatabase();        copyDatabase(db.getPath());    }    return super.getReadableDatabase();}@Overridepublic void onCreate(SQLiteDatabase db) {}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}private void copyDatabase(String dbPath){    try{        InputStream assestDB = context.getAssets().open("databases/"+dbName);        OutputStream appDB = new FileOutputStream(dbPath,false);        byte[] buffer = new byte[1024];        int length;        while ((length = assestDB.read(buffer)) > 0) { appDB.write(buffer, 0, length);        }        appDB.flush();        appDB.close();        assestDB.close();    }catch(IOException e){        e.printStackTrace();    }}}

考虑到数据库的文件扩展名是 .db ,并且我的数据库位于 资产/数据库/



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609432.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号