开发者

Android SQLite unable to upgrade database

The onUpgrade method is called every time the phone restarts and I can see that the oldVersion parameter in onUpgrade never gets called. I have tried uninstalling the application but with no success. The weird thing is that the oldVersion is always 6. Or could there be any other reason why my database is gone every time the phone restarts?

private static final int DATABASE_VERSION = 7;

private static class DatabaseHelper extends SQLiteOpenHelper {
    /**
     * jdbc Constructor.
     * 
     * @param context
     */

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

    }

    /**
     * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
     */
    public void onCr开发者_运维技巧eate(SQLiteDatabase db) {
      LogToSD.write((getClass().toString() + " " + LogToSD.getMethodName(2)), "Enter");
      try {
        db.execSQL(DATABASE_CREATE_1);
        db.execSQL(DATABASE_CREATE_2);
        db.execSQL(DATABASE_CREATE_3);
        db.execSQL(DATABASE_CREATE_4);
        db.execSQL(DATABASE_CREATE_5);
      } catch (Exception e) {
        logger.error("Unable to query database", e);
      }
    }

    /**
     * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase,
     *      int, int)
     */
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      LogToSD.write((getClass().toString() + " " + LogToSD.getMethodName(2)), "Enter");
      String version = "oldVersion " + Integer.toString(oldVersion) + " newVersion "
        + Integer.toString(newVersion);
      LogToSD.write((getClass().toString() + " " + LogToSD.getMethodName(2)), version);


      if (newVersion > oldVersion) {
        try {
          //DROP OLD
          db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_1);
          db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_2);
          db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_3);
          db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_4);
          db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_5);
          //CREATE NEW
          onCreate(db);
        } catch (Exception e) {
          e.toString();
        }
      }

    }
  }

SOLVED So it looked like they were doing nothing in the constructor, but moved everything into a initialize method.

Class Store {
 public Store() {
    db = null;
    table = new HashMap<String, String>();
  }

 public void initialize(Context context) throws SQLException {
    if (db == null) {
      dbHelper = new DatabaseHelper(context);
      db = dbHelper.getWritableDatabase();
    }
  }
}

They did this because they were using a factory that would only create a single instance of the Store if it did not exist, but they could not get the context from the factory. So what they did was call the initialize method from one of the activities. So by moving those things into the constructor and removing the initialize method everything worked. I noticed that the initialize method was called from more than one place. If I understand correctly, it doesn't matter how many times getWritableDatabase is called because the database is a singleton correct?

So my question is, even though this might not be the best practice, why did this not work?

Class Store { public Store(Context context) { dbHelper = new DatabaseHelper(context); db = dbHelper.getWritableDatabase(); } }


Change this:

    } catch (Exception e) {
      e.toString();
    }

To this:

    } catch (Exception e) {
      logger.error("Unable to upgrade database", e);
    }

The stack trace printed should explain your problem. If it doesn't, post it here and we'll take a look. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜