我如何在android中替换我的数据库文件?

时间:2012-12-03 12:55:01

标签: android sqlite

我正在尝试使用新的数据库文件在商店中更新我的应用程序....但是当我尝试运行它时出错...

我认为我的代码存在问题......

    class DatabaseUtilities  {
 public static void createDatabaseIfNotExists(Context context)
            throws IOException {
        boolean createDb = false;
        File dbDir = new File(AppConstants.DB_PATH);
        File dbFile = new File(AppConstants.DB_PATH + AppConstants.DB_NAME);
        if (!dbDir.exists()) {
            dbDir.mkdir();
            createDb = true;
        } else if (!dbFile.exists()) {
            createDb = true;
        } else {
            boolean doUpgrade = true;

            if (doUpgrade) {
                dbFile.delete();
                createDb = true;

            }
        }

        if (createDb) {
            InputStream myInput = context.getAssets().open("altibbi.db");
            OutputStream myOutput = new FileOutputStream(dbFile);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }
    }

    public static SQLiteDatabase getDatabase() {
        return SQLiteDatabase.openDatabase(AppConstants.DB_PATH
                + AppConstants.DB_NAME, null,
                SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    }

 }

1 个答案:

答案 0 :(得分:0)

为什么不使用SQLiteOpenHelper及其onUpgrade()回调来简化代码?