2011-07-09 40 views
0

我想將數據庫升級到更新的版本。我會如何在升級方法中做這件事?如何用此代碼升級SQLDatabase?

private static final String DATABASE_NAME = " nba"; 
private static final String DATABASE_TABLE = "bookList"; 
private static final int DATABASE_VERSION = 1; 
private static String TAG = "Upgrading Database!"; 
public static final String KEY_BOOK = "book"; 
public static final String KEY_AUTHOR = "author"; 
public static final String KEY_ISBN = "isbn"; 
public static final String KEY_ROWID = "_id"; 
public static final String KEY_RATING = "rating"; 
public static final String KEY_STATUS = "status"; 

private DatabaseHelper mDbHelper; 
private SQLiteDatabase mDb; 


private static final String DATABASE_CREATE = 
     " create table " + DATABASE_TABLE + " (" 
     + KEY_ROWID + " integer primary key autoincrement, " 
     + KEY_AUTHOR + " text not null, " 
     + KEY_BOOK + " text not null, " 
     + KEY_RATING + " text not null, " 
     + KEY_STATUS + " text not null, " 
     + KEY_ISBN + " text not null); "; 

private final Context mCtx; 


public DbAdapter (Context ctx){ 
    this.mCtx = ctx; 



     } 

     private static class DatabaseHelper extends SQLiteOpenHelper{ 
     DatabaseHelper(Context context){ 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 


     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
     db.execSQL(DATABASE_CREATE); 


     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
       Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
         + newVersion + ", which will destroy all old data"); 
       db.execSQL("DROP TABLE IF EXISTS user"); 
       db.execSQL("DROP TABLE IF EXISTS site"); 
       db.execSQL("DROP TABLE IF EXISTS site_user"); 
       db.execSQL("DROP TABLE IF EXISTS article"); 
       onCreate(db); 
    } 
} 
     public DbAdapter open() throws SQLException{ 

      mDbHelper = new DatabaseHelper(mCtx); 
      mDb = mDbHelper.getWritableDatabase(); 
      return this; 
     } 
     public void close(){ 
      mDbHelper.close(); 
     } 

     public long createBook(String book, String author, String isbn, float rating, String status){ 
      ContentValues initialValues = new ContentValues(); 
      initialValues.put(KEY_BOOK, book); 
      initialValues.put(KEY_AUTHOR, author); 
      initialValues.put(KEY_ISBN, isbn); 
      initialValues.put(KEY_RATING, rating); 
      initialValues.put(KEY_STATUS, status); 

      return mDb.insert(DATABASE_TABLE, null, initialValues); 

     } 
     public boolean deleteBook(long rowId){ 
      return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 

     } 
     public Cursor fetchAllBooks(){   
      return mDb.query(DATABASE_TABLE, new String[]{KEY_BOOK, KEY_ISBN,KEY_AUTHOR,KEY_ROWID, KEY_RATING, KEY_STATUS}, null, null, null, null, null); 
     } 
     public Cursor fetchBook(long rowId) throws SQLException{ 
      Cursor mCursor = 
      mDb.query(DATABASE_TABLE, new String[]{KEY_BOOK, KEY_AUTHOR,KEY_ROWID, KEY_ISBN, KEY_RATING, KEY_STATUS}, KEY_ROWID + "=" + 
         rowId, null, null, null, null); 

      if(mCursor != null){ 
       mCursor.moveToFirst(); 
      } 
      return mCursor; 

     } 
     public boolean updateBook(long rowId, String book, String author, String isbn, float rating, String status){ 
      ContentValues args = new ContentValues(); 
      args.put(KEY_BOOK, book); 
      args.put(KEY_AUTHOR, author); 
      args.put(KEY_ISBN, isbn); 
      args.put(KEY_RATING, rating); 
      args.put(KEY_STATUS, status); 

      return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null)> 0; 


     } 


} 
+0

你在裏面有代碼已經,你可以更具體一點嗎? –

+0

我想添加另一列,但它似乎會更好只是升級數據庫。但我在那裏只有地方控股。我只想知道我將如何升級我的數據庫。這就是那裏的代碼。 – yoshi24

+0

我不想擦除我的數據那已經在我的模擬器上的表。只是想升級它。 – yoshi24

回答

1

有沒有「神奇的子彈」升級數據庫。您目前的實施策略是正確的,除非它會吹走所有的用戶數據。最優雅但最複雜的解決方案是通過創建一個特殊表來保存該值,從而爲數據庫分配一個版本號。然後,根據版本,onUpgrade方法執行一系列alter語句來修改結構以符合新版本。我會逐步迭代地做到這一點,以便每個版本只需要知道如何從以前的版本升級。

也就是說,版本3不知道如何從版本1升級到版本2,但它確實知道如何從版本2升級到版本3.或者,如果新版本是版本5並且您在版本1,運行腳本以從1升級到2,然後從2升級到3,然後從3升級到4,最後從4升級到5.這是否合理?

如果你問有關如何修改表結構的SQL,檢查出ALTER TABLE statement.

+0

謝謝,所以如果我想使用ALTER TABLE我將如何使用它爲我的代碼上面..要添加另一列呢? – yoshi24