jeudi 31 mars 2016

SQLiteOpenHelper switching database version

Im trying to switch from different database versions. However, the application keeps crashing when I try to access lower database version when my recent access was a higher database version.

Here is the code

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;
    }

Now, I have 5 java file of these and each have a different database version. When Im switching databases the application keeps crashing. Is there anyway to fix this?

Here is the 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)

Aucun commentaire:

Enregistrer un commentaire