将数据库从assets文件夹复制到本地存储错误

时间:2016-06-08 10:03:46

标签: android sqlite

当用户安装我的Android应用程序时,会将sqlite数据库从assets文件夹复制到设备的本地数据库文件夹。我使用此代码来实现:

   private void copyDataBase() throws IOException {

   String DB_PATH = "";
   String DB_NAME = "mydb.db";

    if(android.os.Build.VERSION.SDK_INT >= 17){

        DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
    }
    else
    {
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    }

    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();

  }

这在alle设备上运行良好,除了一个:Moto 3g设备。在没有进一步信息的情况下复制数据库时,这种设备总是会产生一种感觉。

任何人都知道问题是什么?

3 个答案:

答案 0 :(得分:1)

试用此代码它的工作完美只需更改您的表名和包名称

   public class SqlLiteDataBaseHelper extends SQLiteOpenHelper{
    private static final String TAG = SqlLiteDataBaseHelper.class.getSimpleName();
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_PATH = "/data/data/YOUR PACKAGE NAME/databases/";
    private static final String DATABASE_NAME = "test1.sqlite";
    private static final String TABLE_NAME = "Student";
    private static final String COL_Name = "Name";
    private static final String COL_ROLL_NO ="RollNo";
    private Context context;
    private SQLiteDatabase db;

    public SqlLiteDataBaseHelper(Context context) {
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }


    //This method is just retuning total no of recode in your table Getting single contact count
    public int  getDataCount() {

        String userRollNo = null;

        String query = "SELECT * FROM " + TABLE_NAME ;

        Cursor cursor = db.rawQuery(query, null);

        return cursor.getCount();
    }




    public void openDataBase () throws SQLException{
        String path = DATABASE_PATH+DATABASE_NAME;
        db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
    }

    public void CopyDataBaseFromAsset() throws IOException {
        InputStream in  = context.getAssets().open(DATABASE_NAME);
        Log.e("sample", "Starting copying");
        String outputFileName = DATABASE_PATH+DATABASE_NAME;
        File databaseFile = new File( "/data/data/YOUR PACKAGE NAME/databases");
        // check if databases folder exists, if not create one and its subfolders
        if (!databaseFile.exists()){
            databaseFile.mkdir();
        }

        OutputStream out = new FileOutputStream(outputFileName);

        byte[] buffer = new byte[1024];
        int length;


        while ((length = in.read(buffer))>0){
            out.write(buffer,0,length);
        }
        Log.e("sample", "Completed" );
        out.flush();
        out.close();
        in.close();

    }

    public void deleteDb() {
        File file = new File(DATABASE_PATH);
        if(file.exists()) {
            file.delete();
            Log.d(TAG, "Database deleted.");
        }
    }
    public boolean checkDataBase() {
        boolean checkDB = false;
        try {
            File file = new File(DATABASE_PATH);
            checkDB = file.exists();
        } catch(SQLiteException e) {
            Log.d(TAG, e.getMessage());
        }
        return checkDB;
    }
}

在您的行动中使用

sqlLiteDataBaseHelper = new SqlLiteDataBaseHelper(this);


        try {

            if(sqlLiteDataBaseHelper.checkDataBase()){

                Log.e(TAG,"Data Base Already Exists");

            }else {

                sqlLiteDataBaseHelper.CopyDataBaseFromAsset();

            }

        }catch (Exception e){
            e.printStackTrace();
        }
        try {

            sqlLiteDataBaseHelper.openDataBase();
             // after open data  base u can read write data base 

        }catch (Exception e){
            e.printStackTrace();
        }

答案 1 :(得分:0)

请在库下面试试

https://github.com/jgilfelt/android-sqlite-asset-helper

它可能会有所帮助。

答案 2 :(得分:0)

查看此库。 Sqlite Asset helper。当您想要使用apl

发送本地数据库时,它可以大大减少您必须经历的所有烦恼