gpt4 book ai didi

android - onUpgrade() 方法无法正常工作

转载 作者:行者123 更新时间:2023-11-30 04:03:15 28 4
gpt4 key购买 nike

我正在尝试调用 onUpgrade() 来更新我的包含几列的表格。然而,当我运行该应用程序时,我在插入语句中收到一条错误消息,指出该表中不存在列(我尝试在 onUpgrade() 中添加的列)。非常感谢您的帮助。这是我的 SQLite 帮助程序类://更新和工作

public class TasksSQLiteOpenHelper extends SQLiteOpenHelper {
public static final int VERSION = 3;
public static final String DB_NAME = "tasks_db.sqlite";
public static final String TASKS_TABLE = "tasks";
public static final String TASK_ID = "id";
public static final String TASK_NAME = "name";
public static final String TASK_RESPONSIBLE = "responsible";
public static final String TASK_PRIORITY = "priority";
public static final String TASK_DATETIME = "taskdatetime";


public TasksSQLiteOpenHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
createTables(db);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

Log.v("test",Integer.toString(oldVersion));
Log.v("test", Integer.toString(newVersion));
switch(oldVersion){

case 1: db.execSQL("ALTER TABLE "+ TASKS_TABLE +" ADD COLUMN "+ TASK_RESPONSIBLE +" TEXT");
db.execSQL("ALTER TABLE "+ TASKS_TABLE +" ADD COLUMN "+ TASK_PRIORITY +" TEXT");
Log.v("test","res + pri created");
break;

case 2: db.execSQL("ALTER TABLE "+ TASKS_TABLE +" ADD COLUMN "+ TASK_DATETIME +" TEXT");
Log.v("test","date created");
break;
}
}

protected void createTables(SQLiteDatabase db) {
db.execSQL(
"create table " + TASKS_TABLE+" (" +
TASK_ID + " integer primary key,"+
TASK_NAME + " text"+
");"
);
}

}这是我的应用程序类//更新和工作解决方案

    public class TaskManagerApplication extends Application {

private ArrayList<Task> currentTasks;
public SQLiteDatabase database;

@Override
public void onCreate() {
super.onCreate();
TasksSQLiteOpenHelper helper = new TasksSQLiteOpenHelper(this);
database = helper.getWritableDatabase();
if (null == currentTasks) {
loadTasks();
}
}
private void loadTasks() {
currentTasks = new ArrayList<Task>();
Cursor tasksCursor = database.query(TASKS_TABLE, new String[] {TASK_ID, TASK_NAME,TASK_RESPONSIBLE,TASK_PRIORITY,TASK_DATETIME}, null, null, null, null, null);
tasksCursor.moveToFirst();
Task t;
if (! tasksCursor.isAfterLast()) {
do {
long id = tasksCursor.getLong(0);
String name = tasksCursor.getString(1);
String responsible = tasksCursor.getString(2);
String priority = tasksCursor.getString(3);
String datetime = tasksCursor.getString(4);
t = new Task(name, priority, responsible, datetime);
t.setId(id);
currentTasks.add(t);
} while (tasksCursor.moveToNext());
}
tasksCursor.close();
}

public void setCurrentTask(ArrayList<Task> currentTask) {
this.currentTasks = currentTask; // set tasks in array
}

public ArrayList<Task> getCurrentTasks() {
return currentTasks; //call back tasks
}

public void addTask(Task t) { // add new t of type Task
assert(null != t); //check for null entries - avoid crashing
ContentValues values = new ContentValues();
values.put(TASK_NAME, t.getName());
values.put(TASK_PRIORITY, t.getPriority());
values.put(TASK_RESPONSIBLE, t.getResponsible());
values.put(TASK_DATETIME, t.getDatetime());
//Log.v("test", TASK_DATETIME);
t.setId(database.insert(TASKS_TABLE, null, values));
currentTasks.add(t);
}

public void remove(long id){
//remove statement from table using where statement id
database.delete(TASKS_TABLE, TASK_ID +" ="+ id, null);
}

这也是我的 logcat 输出

    08-28 16:30:41.170: I/Database(8931): sqlite returned: error code = 1, msg = table      tasks has no column named datetime
08-28 16:30:41.190: E/Database(8931): Error inserting datetime=Aug 28, 2012 4:30:41 PM responsible=Me priority=Medium name=go chi
08-28 16:30:41.190: E/Database(8931): android.database.sqlite.SQLiteException: table tasks has no column named datetime: , while compiling: INSERT INTO tasks(datetime, responsible, priority, name) VALUES(?, ?, ?, ?);
08-28 16:30:41.190: E/Database(8931): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
08-28 16:30:41.190: E/Database(8931): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
08-28 16:30:41.190: E/Database(8931): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
08-28 16:30:41.190: E/Database(8931): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
08-28 16:30:41.190: E/Database(8931): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
08-28 16:30:41.190: E/Database(8931): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1290)
08-28 16:30:41.190: E/Database(8931): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1710)
08-28 16:30:41.190: E/Database(8931): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1567)
08-28 16:30:41.190: E/Database(8931): at com.id11078995.ghnaim.exercise1.TaskManagerApplication.addTask(TaskManagerApplication.java:64)
08-28 16:30:41.190: E/Database(8931): at com.id11078995.ghnaim.exercise1.AddTaskActivity.addTask(AddTaskActivity.java:59)
08-28 16:30:41.190: E/Database(8931): at com.id11078995.ghnaim.exercise1.AddTaskActivity.onOptionsItemSelected(AddTaskActivity.java:134)
08-28 16:30:41.190: E/Database(8931): at and roid.app.Activity.onMenuItemSelected(Activity.java:2211)
08-28 16:30:41.190: E/Database(8931): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:775)
08-28 16:30:41.190: E/Database(8931): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
08-28 16:30:41.190: E/Database(8931): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:860)
08-28 16:30:41.190: E/Database(8931): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
08-28 16:30:41.190: E/Database(8931): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
08-28 16:30:41.190: E/Database(8931): at android.view.View$PerformClick.run(View.java:9152)
08-28 16:30:41.190: E/Database(8931): at android.os.Handler.handleCallback(Handler.java:587)
08-28 16:30:41.190: E/Database(8931): at android.os.Handler.dispatchMessage(Handler.java:92)
08-28 16:30:41.190: E/Database(8931): at android.os.Looper.loop(Looper.java:130)
08-28 16:30:41.190: E/Database(8931): at android.app.ActivityThread.main(ActivityThread.java:3691)
08-28 16:30:41.190: E/Database(8931): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 16:30:41.190: E/Database(8931): at java.lang.reflect.Method.invoke(Method.java:507)
08-28 16:30:41.190: E/Database(8931): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
08-28 16:30:41.190: E/Database(8931): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
08-28 16:30:41.190: E/Database(8931): at dalvik.system.NativeStart.main(Native Method)

最佳答案

我想这太明显了,但是您是否更改了数据库版本?如果不是,onUpgrade() 方法将不起作用

关于android - onUpgrade() 方法无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12153933/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com