SQLiteOpenHelper切换数据库版本

时间:2016-03-31 15:27:41

标签: android sqlite

我试图从不同的数据库版本切换。但是,当我最近访问较高的数据库版本时,当我尝试访问较低的数据库版本时,应用程序仍然崩溃。

这是代码

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文件,每个都有不同的数据库版本。当我切换数据库时,应用程序不断崩溃。无论如何要解决这个问题吗?

这是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)

1 个答案:

答案 0 :(得分:1)

要在Android中降级SQLite数据库版本,您需要覆盖{ [Error: scripts/main.js: Unexpected token: name (sidr)] message: 'scripts/main.js: Unexpected token: name (sidr)', fileName: 'scripts/main.js', ... showStack: false, showProperties: true, plugin: 'gulp-uglify' } 类的onDowngrade(...)方法。就像你有SQLiteOpenHelper方法一样:

onUpgrade(...)

在它下面添加降级方法

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

但你错误地使用了数据库。不要为每个测验及其问题创建新的DB文件。你只需要一个DB就可以了。甚至不为您的测验创建多个表。所有测验都属于一个表,所有问题都属于另一个表。谷歌一些数据库基础......