为什么每次向sqlite数据库添加新表时都要增加数据库verison?

时间:2016-03-30 06:20:53

标签: android database sqlite

这是我的数据库创建的地方。我使用了一个全局变量,在登录时获取用户名,然后我根据该用户名创建一个表。 当我尝试将数据输入数据库时​​,它给出了一个错误,表明不存在这样的表。如果我手动增加数据库版本,它的工作原理!每次创建新表时都必须增加数据库版本吗?如果是这样,我怎么能实现呢?

很快就会有新用户注册一个新表来保存用户信息,在调试时,我必须手动进入我的代码并在每次添加新用户时增加数据库版本,是否有更好的方法这样做?否则,用户将能够登录但无法输入任何信息,直到我访问我的代码并增加数据库版本..谢谢

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

}

这是我的追踪

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)

1 个答案:

答案 0 :(得分:0)

为了在Android中升级数据库,您应该将DATABASE_VERSION增加1,以便SQLOpenHelper知道它必须调用onUpgrade方法。

否则你的表格不会被添加,旧数据库将被使用。