onUpgrade() を呼び出して、いくつかの列でテーブルを更新しようとしています。ただし、アプリを実行すると、insert ステートメントでエラーが発生し、このテーブルに列が存在しないというエラーが表示されます (onUpgrade() で追加しようとしている列)。 /更新され、機能しています
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"+
");"
);
}
ここに私のApplicationクラスがあります//更新された実用的なソリューション
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)