mardi 29 mars 2016

why do i have to increment my database verison every time i add a new table to my sqlite database? [duplicate]

This question already has an answer here:

This is where my database is created. I used a global variable that takes the username upon login, then i create a table based on that username. when i try to enter data into the database it gives me an error that says no such table exists. if i increment the database version manually, it works! do i have to increment the database version every time i create a new table? if so, how can i achieve that?

package com.example.victor.kidsrewards;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Intent;
import android.os.Bundle;

/**
 * Created by Victor on 3/20/2016.
 */
public class TaskSQLiteHelper extends SQLiteOpenHelper {
    String _username = Globals._username;

    public TaskSQLiteHelper(Context context){
        super(context, "tasks_db", null, 8);

    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        //create a table

        db.execSQL("CREATE TABLE tasks_"+_username + " (_id INTEGER PRIMARY KEY , task TEXT NOT NULL, points INTEGER NOT NULL)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS tasks_"+_username+";");
        this.onCreate(db);
    }

}

this is my trace

03-30 14:18:25.217 6450-6450/com.example.victor.kidsrewards E/SQLiteLog: (1) no such table: tasks_JOE
03-30 14:18:25.217 6450-6450/com.example.victor.kidsrewards D/AndroidRuntime: Shutting down VM
03-30 14:18:25.221 6450-6450/com.example.victor.kidsrewards W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x94d5eb20)
03-30 14:18:25.221 6450-6450/com.example.victor.kidsrewards E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.victor.kidsrewards, PID: 6450
                                                                              android.database.sqlite.SQLiteException: no such table: tasks_JOE (code 1): , while compiling: SELECT _id, task, points FROM tasks_JOE
                                                                                  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                                                  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                                                  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
                                                                                  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
                                                                                  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
                                                                                  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
                                                                                  at com.example.victor.kidsrewards.TaskDataBase.getTasks(TaskDataBase.java:59)
                                                                                  at com.example.victor.kidsrewards.TaskList.onActivityCreated(TaskList.java:46)
                                                                                  at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1983)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1092)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                                  at android.os.Handler.handleCallback(Handler.java:733)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:136)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5045)
                                                                                  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:779)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                                  at dalvik.system.NativeStart.main(Native Method)

Aucun commentaire:

Enregistrer un commentaire