2012-03-23 61 views
2

我有一個測驗遊戲,我保持數據庫高分和不同表中的問題,我使用以下數據庫適配器。如果我用更多的問題更新數據庫,它會覆蓋數據庫,所以它會在高分時做同樣的事情。我怎樣才能讓桌子保持高分,這樣玩家就不會失去它,只更新問題表?從數據庫保留表,同時升級Android

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(); 

    } 
}} 
+0

在onUpgrade期間只放下問題表 – njzk2 2012-03-23 13:55:18

回答

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,以便你可以進行修改。請記住,這僅適用於模式更改。如果您的數據庫保持相同的結構,但您只是添加數據,除了向表中添加新數據之外,不要執行任何操作。

+0

不要再次調用onCreate()。只需執行你想要的查詢。 – kosa 2012-03-23 14:08:44

相關問題