在升级时创建表

时间:2012-04-28 13:55:22

标签: android sqlite

我一直在努力做到这一点,但我不知道我错过了什么。我有一个Android应用程序,我希望再添加1个表。但是我无法做到这一点,我也没有例外(不喜欢这些沉默的杀手!!)。

下面是我的代码我的SQLiteHelper类

public class DbCreator extends SQLiteOpenHelper {

public DbCreator(Context context) {

    super(context, Constants.DB_NAME, null, Constants.NEW_VERSION);//NEW_VERSION=2

    this.myContext = context;
}

//Rest of code

//Checks if DB is present and create if reqd.
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 : " + e);

        }
    }

}

    //Check DB is present
    private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

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

    } catch (SQLiteException e) {
        Log.v("DB", "No DB");
        // database does't exist yet.

    }

    if (checkDB != null) {

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

/**
 * Copies your database from 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.getResources().openRawResource(
            R.raw.diary_database);

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

}

    //**This is the problem area
    @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Constants.log(TAG, "Upgrading started..."+db.getVersion() );
    if (db.getVersion() == Constants.OLD_VERSION) {
            db.beginTransaction();
        Constants.log(TAG, "Upgrading started...transaction");
        db.execSQL("create TABLE Thought(  StartDate DATETIME, Content TEXT, EndDate DATETIME, Title TEXT )");
        db.setVersion(Constants.NEW_VERSION);//NEW_VERSION=2
            db.endTransaction();
        Constants.log(TAG, "Upgrading started...transaction finished");

    }

}

最糟糕的部分是我看到所有日志发生,甚至在我在控制台上执行时查询也会成功运行。


编辑

我的数据库没有更新: -

我已将我的数据库从模拟器中拉出来,甚至看到版本号中的日志没有更改。我在我的活动中使用以下行来查看数据库版本。

DbCreator dbCr = new DbCreator(this);
SQLiteDatabase myDataBase = dbCr.getMyDatabase();
Constants.log(TAG, "Db Version : "+myDataBase.getVersion());

1 个答案:

答案 0 :(得分:5)

我强烈建议您切换到SQLiteAssetHelper,其中包含整个数据包应用程序模式。

从战术上讲,你没有提交你的交易。多语句事务的正确配方是:

try {
  db.beginTransaction();

  // do SQL here

  db.setTransactionSuccessful();
}
finally {
  db.endTransaction();
}

如果没有setTransactionSuccessful()来电,endTransaction()会执行ROLLBACK