2016-03-31 38 views
0

我試圖從不同的數據庫版本切換。但是,當我最近訪問的數據庫版本更高時,當我嘗試訪問較低的數據庫版本時,應用程序不斷崩潰。SQLiteOpenHelper切換數據庫版本

下面是代碼

public class DbHelper extends SQLiteOpenHelper { 
    private static final int DATABASE_VERSION = 46; 
    // Database Name 
    private static final String DATABASE_NAME = "triviaQuiz"; 
    // tasks table name 
    private static final String TABLE_QUEST = "quest"; 
    // tasks Table Columns names 
    private static final String KEY_ID = "id"; 
    private static final String KEY_QUES = "question"; 
    private static final String KEY_ANSWER = "answer"; //correct option 
    private static final String KEY_OPTA= "opta"; //option a 
    private static final String KEY_OPTB= "optb"; //option b 
    private static final String KEY_OPTC= "optc"; //option c 
    private SQLiteDatabase dbase; 
    public DbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     dbase=db; 
     String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " (" 
       + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES 
       + " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, " 
       +KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)"; 
     db.execSQL(sql); 
     addQuestions(); 
     //db.close(); 
    } 
    private void addQuestions() 
    { 
     Question q1=new Question("What does ICT stands for?","Individual Computing Techniques", "Information Computer Technology", "Information Computer Tutorial", "Information Computer Technology"); 
     this.addQuestion(q1); 
     Question q2=new Question("Information Technology is also classified as the Science and Art of?", "Recording and Storage", "Past Time", "Information", "Recording and Storage"); 
     this.addQuestion(q2); 
     Question q3=new Question("Based on the lesson, what are the things that you might experience when interacting with other people in the internet?","Free load", "Time Leisure","Cyber Bullying", "Cyber Bullying"); 
     this.addQuestion(q3); 
     Question q4=new Question("What are the things you need to consider before turning off the computer?", "Make sure to save all your work", "Make sure to log out your account", "Leave a file open","Make sure to log out your account"); 
     this.addQuestion(q4); 
     Question q5=new Question("Password must be?","Easy to guess","Easy to remember","Unique and hard to guess by other people","Unique and hard to guess by other people"); 
     this.addQuestion(q5); 
    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) { 
     // Drop older table if existed 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST); 
     // Create tables again 
     onCreate(db); 
    } 
    // Adding new question 
    public void addQuestion(Question quest) { 
     //SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(KEY_QUES, quest.getQUESTION()); 
     values.put(KEY_ANSWER, quest.getANSWER()); 
     values.put(KEY_OPTA, quest.getOPTA()); 
     values.put(KEY_OPTB, quest.getOPTB()); 
     values.put(KEY_OPTC, quest.getOPTC()); 
     // Inserting Row 
     dbase.insert(TABLE_QUEST, null, values); 
    } 
    public List<Question> getAllQuestions() { 
     List<Question> quesList = new ArrayList<Question>(); 
     // Select All Query 
     String selectQuery = "SELECT * FROM " + TABLE_QUEST; 
     dbase=this.getReadableDatabase(); 
     Cursor cursor = dbase.rawQuery(selectQuery, null); 
     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 
       Question quest = new Question(); 
       quest.setID(cursor.getInt(0)); 
       quest.setQUESTION(cursor.getString(1)); 
       quest.setANSWER(cursor.getString(2)); 
       quest.setOPTA(cursor.getString(3)); 
       quest.setOPTB(cursor.getString(4)); 
       quest.setOPTC(cursor.getString(5)); 
       quesList.add(quest); 
      } while (cursor.moveToNext()); 
     } 
     // return quest list 
     return quesList; 
    } 
    public int rowcount() 
    { 
     int row=0; 
     String selectQuery = "SELECT * FROM " + TABLE_QUEST; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 
     row=cursor.getCount(); 
     return row; 
    } 

現在,我的這5個Java文件,並各自具有不同的數據庫版本。當Im切換數據庫時,應用程序不斷崩潰。有沒有什麼辦法解決這一問題?

這裏是logcat的

03-31 22:45:20.793 25098-25098/? E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.doepiccoding.navigationdrawer, PID: 25098 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.doepiccoding.navigationdrawer/com.android.pet.view.QuizActivity1}: android.database.sqlite.SQLiteException: Can't downgrade database from version 46 to 45 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
    at android.app.ActivityThread.access$800(ActivityThread.java:135) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5021) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 46 to 45 
    at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:361) 
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:255) 
    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188) 
    at com.android.pet.view.DbHelper1.getAllQuestions(DbHelper1.java:80) 
    at com.android.pet.view.QuizActivity1.onCreate(QuizActivity1.java:35) 
    at android.app.Activity.performCreate(Activity.java:5231) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)  
    at android.app.ActivityThread.access$800(ActivityThread.java:135)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:136)  
    at android.app.ActivityThread.main(ActivityThread.java:5021)  
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:515) 
+0

你可以顯示logcat?爲什麼你有不同的數據庫版本? –

+0

顯然,因爲默認實現'SQLiteOpenHelper.onDowngrade',所以你沒有重寫這個方法... – Selvin

+0

@ cricket_007已經更新。因爲我存儲問題到我的數據庫 – Dreamer

回答

1

要降級Android中一個SQLite數據庫版本uou需要重寫SQLiteOpenHelper類的onDowngrade(...)方法。就像你有你的onUpgrade(...)方法:

@Override 
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) { 
... 
} 

添加它下面的降級方法

@Override 
public void onDowngrade (SQLiteDatabase db, int oldVersion, int newVersion) { 
// your DB dowgrade code 
} 

但你餘呂杏茜在一個錯誤的錯誤使用數據庫。不要爲每個測驗和他們的問題創建一個新的數據庫文件。一切只需要一個數據庫。甚至不要爲您的測驗創建多個表格。所有測驗都屬於一個表格,所有問題都屬於另一個表格。谷歌一些數據庫的基礎知識...