2012-03-23 2 views
2

퀴즈 게임이 있는데 데이터베이스에 높은 점수와 다른 테이블의 질문을 유지하면서 다음 db 어댑터를 사용합니다. 더 많은 질문으로 데이터베이스를 업데이트하면 데이터베이스를 덮어 쓰게되므로 높은 점수로 처리됩니다. 플레이어가 점수를 잃어 버리지 않고 질문 표만 업데이트 할 수 있도록 어떻게 높은 점수를 유지할 수 있습니까?업그레이드하는 동안 데이터베이스에서 테이블을 유지하십시오.

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

답변

1

DatabaseHelper는 onCreate를 호출하는 대신 onUpgrade를 구현해야합니다. 귀하의 코드 :

대신에 주석 처리 한 db.execSQL 행에서 원하는 테이블을 수정하려면 SQL을 작성하십시오. OS는 oldVersion과 newVersion을 전달하여 수정할 수 있습니다. 이 변경 사항은 스키마 변경에만 해당됩니다. 데이터베이스가 동일한 구조를 유지하지만 데이터를 추가하는 경우 테이블에 새로운 데이터를 추가하는 것 외에는 아무 것도하지 마십시오.

+0

동안 질문 테이블 만 삭제하면 onCreate()가 다시 호출되지 않습니다. 원하는 쿼리를 실행하기 만하면됩니다. – kosa

관련 문제