【问题标题】:Keep table from database while upgrade Android升级Android时保留数据库中的表
【发布时间】:2012-03-23 13:51:51
【问题描述】:

我有一个问答游戏,我将数据库中的高分和不同表格中的问题保存在一起,我使用以下数据库适配器。如果我用更多问题更新数据库,它将覆盖数据库,因此它会以高分做同样的事情。我怎样才能保持高分表让玩家不会丢失它,并且只更新问题表?

public class AnyDBAdapter {

private static final String TAG = "AnyDBAdapter";
private DatabaseHelper mDbHelper;
private static SQLiteDatabase mDb;

private static String DB_PATH = "/data/data/xx.xxxxx.xxxx/databases/";
private static final String DATABASE_NAME = "dbname";

private static final int DATABASE_VERSION = 1;

private final Context adapterContext;

public AnyDBAdapter(Context context) {
    this.adapterContext = context;
}

public AnyDBAdapter open() throws SQLException {
    mDbHelper = new DatabaseHelper(adapterContext);

    try {
        mDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }

    try {
        mDbHelper.openDataBase();
    } catch (SQLException sqle) {
        throw sqle;
    }
    return this;
}

public void close() {
    mDbHelper.close();
}

private static class DatabaseHelper extends SQLiteOpenHelper {

    Context helperContext;

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        helperContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database!!!!!");
        //db.execSQL("");
        onCreate(db);
    }

    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();
        SQLiteDatabase db_Read = null;
        if (dbExist) {
        } else {
            db_Read = this.getReadableDatabase();
            db_Read.close();
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }

    public SQLiteDatabase getDatabase() {
        String myPath = DB_PATH + DATABASE_NAME;
        return SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            String myPath = DB_PATH + DATABASE_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {
        }
        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

    private void copyDataBase() throws IOException {

        // Open your local db as the input stream
        InputStream myInput = helperContext.getAssets().open(DATABASE_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DATABASE_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();
    }

    public void openDataBase() throws SQLException {
        // Open the database
        String myPath = DB_PATH + DATABASE_NAME;
        mDb = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    }

    @Override
    public synchronized void close() {

        if (mDb != null)
            mDb.close();

        super.close();

    }
}}

【问题讨论】:

  • 在 onUpgrade 期间只删除问题表

标签: android database


【解决方案1】:

您的 DatabaseHelper 需要实现 onUpgrade 而不是仅仅调用 onCreate。你的代码:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(TAG, "Upgrading database!!!!!");
    //db.execSQL("");
    onCreate(db);
}

相反,编写一些 SQL 来修改您已注释掉的 db.execSQL 行中所需的表。操作系统会将 oldVersion 和 newVersion 传递给您,以便您进行修改。请记住,这仅适用于架构更改。如果您的数据库保持相同的结构,但您只是在添加数据,那么除了向表中添加新数据之外不要做任何事情。

【讨论】:

  • 不要再次调用 onCreate()。只需在那里执行您想要的查询。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多