2

我一直在努力做到这一点,但我不知道我错过了什么。我有一个 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());
4

1 回答 1

5

我强烈建议您切换到SQLiteAssetHelper,它已经制定了整个应用程序包中的数据库模式。

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

try {
  db.beginTransaction();

  // do SQL here

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

没有setTransactionSuccessful()调用,endTransaction()会做一个ROLLBACK.

于 2012-04-28T15:19:57.593 回答