2

很抱歉这样一个蹩脚的问题,但我无法解决 4 小时。我正在尝试从可以与 SQLiteOpenHelper 一起使用的 /assets 文件夹复制数据库,但是当我尝试打开 InputStream 时,它给了我错误:

 E/Trace: error opening trace file: No such file or directory (2)

代码:

public static final String DATABASE_PATH =  "/data/data/com.mycomp.myapp/databases/";
public static final String DATABASE_NAME = "database.db"; 

@Override
    protected void onCreate(Bundle savedInstanceState) {   
        try {
            copyDatabase(getApplicationContext());
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "blad" + e.toString());
        }
    }

    private void copyDatabase(Context context) throws IOException {
       String outfilename = DATABASE_PATH + DATABASE_NAME;

       InputStream myinput = context.getAssets().open("database.db");

       OutputStream myoutput = new FileOutputStream(outfilename);

       byte[] buffer = new byte[1024];
       int length;
       while ((length = myinput.read(buffer)) > 0) {
          myoutput.write(buffer, 0, length);
      }

       myoutput.flush();
       myoutput.close();
       myinput.close();
    }
4

2 回答 2

2

你能试试这个代码,然后请告诉我结果。确保检查所有文件名。

@Override
protected void onCreate(Bundle savedInstanceState) {   
    //.......

    DatabaseHelper dbHelper = new DatabaseHelper(this);
    dbHelper.createDatabase();

    dbHelper.openDatabase();
    // do stuff
    Cursor data =dbHelper.Sample_use_of_helper();
    dbHelper.close();
}


class DatabaseHelper extends SQLiteOpenHelper {

    private static String DB_PATH = "/data/data/com.mycomp.myapp/databases/";
    private static String DB_NAME = "database.db";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

        public DatabaseHelper (Context context) {
            super(context, DB_NAME, null, 1);
            this.myContext = context;
        }

        public void crateDatabase() throws IOException {
            boolean vtVarMi = isDatabaseExist();

            if (!vtVarMi) {
                this.getReadableDatabase();

                try {
                    copyDataBase();
                } catch (IOException e) {
                    throw new Error("Error copying database");
                }
            }
        }

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

        private boolean isDatabaseExist() {
            SQLiteDatabase kontrol = null;

            try {
                String myPath = DB_PATH + DB_NAME;
                kontrol = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

            } catch (SQLiteException e) {
                kontrol = null;
            }

            if (kontrol != null) {
                kontrol.close();
            }
            return kontrol != null ? true : false;
        }

        public void openDataBase() throws SQLException {

            // Open the database
            String myPath = DB_PATH + DB_NAME;
            myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

        }

        public Cursor Sample_use_of_helper() {

            return myDataBase.query("TABLE_NAME", null, null, null, null, null, null);
        }

        @Override
        public synchronized void close() {
            if (myDataBase != null)
                myDataBase.close();
            super.close();
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }
于 2013-05-20T07:14:28.107 回答
0

DB_NAME.open("database.db")中删除.db扩展名 它看起来很难看,但目前同样的事情对我有用.... 在安装新的 apk 之前,请为早期安装的 apk 执行清除数据

如果它仍然不起作用,请提及您的数据库大小。

于 2013-05-20T07:47:23.413 回答