本文介绍了在Android的数据文件夹拷贝数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用这code从资产复制我的SQLite数据库中的数据机器人。
私有静态字符串DB_PATH =/data/data/com.project.writing/databases/; 私有静态字符串DB_NAME =Fonts.sqlite; 私人SQLiteDatabase schoolDataBase; / ** *在系统上创建一个空数据库,并与自己的数据库重写它。 * * / 公共无效的CreateDatabase()抛出IOException异常{ 布尔dbExist = checkDataBase(); 如果(dbExist){ //什么也不做 - 数据库中已存在 }其他{ //通过调用这个方法和空的数据库将被创建到系统默认路径 //你的应用程序,所以我们要能够覆盖数据库,我们的数据库。 this.getReadableDatabase(); 尝试 { copyDataBase(); }赶上(IOException异常E){ 抛出新的错误(错误复制数据库); } } } / ** *检查数据库中已存在,以避免重新复制每次打开应用程序时,该文件。 * @返回true,如果它存在,虚假的,如果它不 * / 私人布尔checkDataBase(){ SQLiteDatabase CHECKDB = NULL; 尝试{ 字符串mypath中= DB_PATH + DB_NAME; CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY); }赶上(SQLiteException E){ //数据库的简化版,存在。 的System.out.println(数据库不存在+ E); } 如果(CHECKDB!= NULL){ checkDB.close(); } 返回CHECKDB!= NULL?真假; } / ** *复制你的数据库从当地的资产文件夹在刚才创建的空数据库 *系统文件夹,从那里它可以被访问和处理。 *这是通过transfering字节流进行。 * * * / 私人无效copyDataBase()抛出IOException异常{ //打开本地数据库作为输入流 InputStream的myInput = myContext.getAssets()开(DB_NAME)。 //路径刚刚创建的空分贝 字符串outFileName = DB_PATH + DB_NAME; //打开空分贝的输出流 的OutputStream myOutput =新的FileOutputStream(outFileName); //传输的字节从inputfile中的OUTPUTFILE byte []的缓冲区=新的字节[1024]; INT长; 而((长度= myInput.read(缓冲液))大于0){ myOutput.write(缓冲液,0,长度); } //关闭流 myOutput.flush(); myOutput.close(); myInput.close(); //Toast.makeText(myContext,复制完成,300).show(); }解决方案
不要推倒重来。有好的包,确实要在2号线的code什么: https://开头github/jgilfelt/android-sqlite-asset-helper
I am using this code copy my sqlite database from asset to data in android .
private static String DB_PATH= "/data/data/com.project.writing/databases/"; private static String DB_NAME = "Fonts.sqlite"; private SQLiteDatabase schoolDataBase; /** * Creates a empty database on the system and rewrites it with your own database. * */ public void createDataBase() throws IOException{ boolean dbExist = checkDataBase(); if(dbExist){ //do nothing - database already exist }else{ //By calling this method and empty database will be created into the default system path //of your application so we are gonna be able to overwrite that database with our database. this.getReadableDatabase(); try { copyDataBase(); } catch (IOException e) { throw new Error("Error copying database"); } } } /** * Check if the database already exist to avoid re-copying the file each time you open the application. * @return true if it exists, false if it doesn't */ private boolean checkDataBase(){ SQLiteDatabase checkDB = null; try{ String myPath = DB_PATH + DB_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); }catch(SQLiteException e){ //database does't exist yet. System.out.println("data base does not exist"+e); } if(checkDB != null){ checkDB.close(); } return checkDB != null ? true : false; } /** * Copies your database from your local assets-folder to the just created empty database in the * system folder, from where it can be accessed and handled. * This is done by transfering bytestream. * * */ private void copyDataBase() throws IOException{ //Open your local db as the input stream InputStream myInput = myContext.getAssets().open(DB_NAME); // Path to the just created empty db String outFileName = DB_PATH + DB_NAME; //Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(outFileName); //transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } //Close the streams myOutput.flush(); myOutput.close(); myInput.close(); //Toast.makeText(myContext, "Copy Done", 300).show(); }解决方案
Do not reinvent the wheel. There's nice package that does what you want in 2 lines of code: github/jgilfelt/android-sqlite-asset-helper
更多推荐
在Android的数据文件夹拷贝数据库
发布评论